How do I find out why g++ takes a very long time on a particular file?

后端 未结 8 1372
日久生厌
日久生厌 2021-02-01 21:51

I am building a lot of auto-generated code, including one particularly large file (~15K lines), using a mingw32 cross compiler on linux. Most files are extremely quick, but thi

相关标签:
8条回答
  • 2021-02-01 22:18

    Related to @Goz and @Josh_Kelley, you can get gcc/g++ to spit out the preprocessed source (with #includes inline) using -E. That's one way to determine just how large your source is.

    And if the compiler itself is the problem, you may be able to strace the compile command that's taking a long time to see whether there's a particular file access or a specific internal action that's taking a long time.

    0 讨论(0)
  • 2021-02-01 22:27

    What slows g++ down in general are templates. For example Boost loves to use them. This means nice code, great performances but poor compiling speed.

    On the other hand, 15min seems extremely long. After a quick googling, it seems that it is a common problem with mingw

    0 讨论(0)
  • 2021-02-01 22:28

    Another process to try is to add "progress marker" pragmas to your code to trap the portion of the code that is taking a long time. The Visual Studio compiler provides #pragma message(), although there is not a standard pragma for doing this.

    Put one marker at the beginning of the code and a marker at the end of the code. The end marker could be a #error since you don't care about the remainder of the source file. Move the markers accordingly to trap the section of code taking the longest time.

    Just a thought...

    0 讨论(0)
  • 2021-02-01 22:32

    I'd use #if 0 / #endif to eliminate large portions of the source file from compilation. Repeat with different blocks of code until you pinpoint which block(s) are slow. For starters, you can see if your #include's are the problem by using #if 0 / #endif to exclude everything but the #include's.

    0 讨论(0)
  • 2021-02-01 22:32

    One thing to watch during the compile is how much memory your computer has free. If the compiler allocates so much memory that the computer starts swapping, compile time will go way, way up.

    If you see that happen, an easily solution is to install more RAM... or just split the file into multiple parts that can be compiled separately.

    0 讨论(0)
  • 2021-02-01 22:33

    Won't give all the details you want, but try running with the -v (verbose) and -ftime-report flags. The latter produces a summary of what the compiler has been up to.

    0 讨论(0)
提交回复
热议问题