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

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 this one large file takes an unexpectedly long time (~15 minutes) to compile.

I have tried manipulating various optimization flags to see if they had any effect, without any luck. What I really need is some way of determining what g++ is doing that is taking so long. Are there any (relatively simple) ways to have g++ generate output about different phases of compilation, to help me narrow down what the hang-up might be?

Sadly, I do not have the ability to rebuild this cross-compiler, so adding debugging information to the compiler and stepping through it is not a possibility.

What's in the file:

  • a bunch of includes
  • a bunch of string comparisons
  • a bunch of if-then checks and constructor invocations
  • The file is a factory for producing a ton of different specific subclasses of a certain parent class. Most of the includes, however, are nothing terribly fancy.


    The results of -ftime-report, as suggested by Neil Butterworth, indicate that the "life analysis" phase is taking 921 seconds, which takes up most of the 15 minutes.

    It appears that this takes place during data flow analysis. The file itself is a bunch of conditional string comparisons, constructing an object by class name provided as a string.

    We think changing this to point into a map of names to function pointers might improve things a bit, so we're going to try that.


    Indeed, generating a bunch of factory functions (per object) and creating a map from the string name of the object to a pointer to its factory function reduced compile time from the original 15 minutes to about 25 seconds, which will save everyone tons of time on their builds.

    Thanks again to Neil Butterworth for the tip about -ftime-report.


    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.


    It most probably includes TONNES of includes. I believe -MD will list out all the include files in a given CPP file (That includes includes of includes and so forth).


    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

    链接地址: http://www.djcxy.com/p/14980.html

    上一篇: C ++的哪些特性在编译时特别占用资源?

    下一篇: 我如何知道为什么g ++在特定文件上花费很长时间?