1

My code has been taking a long time to compile (around 10 minutes). I removed parts of the code to identify the problem, until all that was left was the declaration the global variable Znodal[dim3][dim3] and the main function with hello world code.

I noticed that when removing the complex array Znodal[dim3][dim3] the compile time goes back to normal, taking 0.81 seconds to compile. Therefore, the problem seems to be related to the complex number library. However, this code ran normally on another computer. So, it looks like it's a compiler configuration issue.

Has anyone had a similar problem or can give me some tips to solve this problem?

The code below takes 10 minutes to compile on my laptop.

#include <complex>

#define dim3 435
std::complex<double> Znodal[dim3][dim3];

#if 0
#include <iostream>
int main() {
    std::cout << "Hello World!\n";
    return 0;
}
#endif

A 1D array of the same size (std::complex<double> Znodal[dim3][dim3];) takes a similarly long time to compile with this version of MSVC. (Testing on Godbolt shows MSVC 19.14 through 19.24 compiled quickly as expected for a zero-init array, with default options and -O2 optimization level, but MSVC 19.25 and later are so slow they time out. The long compile times led to MS's servers blocking requests temporarily so it's hard to experiment with; the Godbolt compiler explorer bounces requests to MS's servers for MSVC other than the WINE install.)


My laptop setup is IdeaPad Gaming 3 15IMH05, I5-10300H, CPU 2.5GHz, 16GB RAM, SSD 500GB WD Blue SN570, M.2 2280, NVMe, GTX 1650, Windows 10 Home Single Language, version 22H2.

Microsoft Visual Studio Community 2022 (64 bits) - Version 17.6.2.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
CReiz
  • 41
  • 3
  • 4
    Wow, so many macros (`#define`) - *why*? And global variables - again, *why*? – Jesper Juhl Aug 21 '23 at 19:56
  • 2
    Why "C" style arrays, why so many global variables... why the total lack of (semblance) of design? – Pepijn Kramer Aug 21 '23 at 19:56
  • 5
    0.378 seconds with g++ on Fedora. But yeah, to reiterate the above comments, whatever book taught you to program with global variables and a boatload of `#define`s, please ceremoniously burn it in holy fire, because that's not how modern C++ works. At all. – Silvio Mayolo Aug 21 '23 at 19:58
  • 1
    The compile time is probably due to the large global variables. The global variables and the #defines are not good style / not recommended. – drescherjm Aug 21 '23 at 19:58
  • 1
    Also do not ignore warnings like you do. Enable even more warnings if you can, they should not be ignored. – Pepijn Kramer Aug 21 '23 at 20:01
  • 2
    "Warnings should never be ignored" - should *usually* not be ignored. If you are adding someone else's library directly into a project you might not have the luxury of re-writing their code to fix warnings. That said, the warnings shouldn't be disabled until after you've looked at them once. – Dave S Aug 21 '23 at 20:01
  • 1
    If I ever saw something like this in a (professional) code review, it would take about 2 seconds to just say "No, no, *hell no*" and I'm not bothering to read any further. – Jesper Juhl Aug 21 '23 at 20:07
  • 1
    [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Aug 21 '23 at 20:09
  • 2
    Please do more experiments with tweaking the code and report results. Some ideas on what to delete: macros; small arrays; `using` directive; `#include` directives; please also try to tweak the project file `*.vcproj` or replace it with a new one. Please try to make a *minimal* code example which still reproduces the problem. You will discover some clues when doing this. Please [edit] your question and describe what you found. – anatolyg Aug 21 '23 at 20:18
  • 1
    Only takes 8 minutes for me. *Winning!* – Retired Ninja Aug 21 '23 at 20:46
  • Thank you all for the comments. I thought it would take days to get any response. I reduced the code as much as possible and now includes only the Znodal complex matrix. Still, the compiler takes about ten minutes. Silvio Mayolo's build time indicates an issue with my compiler, right? Also, @Retired Ninja seems to have the same problem. – CReiz Aug 21 '23 at 21:22
  • 2
    https://godbolt.org/z/xvjY6cdo8 confirms MSVC 19.35 takes a really long time to compile with this medium-sized 2D array of `std::complex`, although MSVC 19.14 (VS2017) under WINE finished as fast as clang (about a second or 2 total including network round-trip and asm parsing) for an optimized (-O2) or debug build. (19.35 on MS's Windows servers timed out; Godbolt sends requests to MS's servers for the non-WINE MSVC compilers.) It does emit a "constructor" that loops over the array to zero it (inefficiently with scalar stores) instead of just using zero-init BSS space like GCC/clang. – Peter Cordes Aug 21 '23 at 21:34
  • 1
    I got 15:46.460 minutes on a i7-8750H CPU 6C / 12T MSI gaming laptop with 17 inch display using VS2022. – drescherjm Aug 21 '23 at 21:57
  • 1
    Thank you for your answer @Peter Cordes. The compiler was changed from the standard to clang. Now, the same code compile in 2 seconds. It's not necessarily the answer to the problem, but it's an effective way to get around it. – CReiz Aug 22 '23 at 01:04

0 Answers0