How to speed up g++ compile time (when using a lot

2019-01-29 23:47发布

This question is perhaps somehow odd, but how can I speed up g++ compile time? My C++ code heavily uses boost and templates. I already moved as much as possible out of the headers files and use the -j option, but still it takes quite a while to compile (and link).

Are there any tools out there which analyse my code and point out bottle-necks for the compiler? Or can one somehow profile the compiler running on my code? This would be really nice, because sometimes I have the impression, that I spent too much time staring at the compiler console log ...

11条回答
看我几分像从前
2楼-- · 2019-01-30 00:20

Try the PIMPL technique, this question: What techniques can be used to speed up C++ compilation times?

It'll prevent the compiler from following the chain of header files and implementations every time you need to do something.

查看更多
戒情不戒烟
3楼-- · 2019-01-30 00:23

Usually, the most expensive parts of compilation are (a) reading the source files (ALL of them) and (b) loading the compiler into memory for each source file.

If you have 52 source (.cc) files, each of which #includes 47 #include (.h) files, you are going to load the compiler 52 times, and you are going to plow through 2496 files. Depending on the density of comments in the files, you may be spending a fair chunk of time eating useless characters. (In one organization I have seen, header files varied between 66% and 90% comments, with only 10%-33% of the file being "meaningful". The single best thing that could be done to enhance readability of those files was strip out every last comment, leaving only code.)

Take a long look at how your program is physically organized. See whether you can combine source files, and simplify your hierarchy of #include files.

Decades ago, companies like IBM understood this, and would write their compilers so that the compiler could be handed a list of files to compile, not just one file, and the compiler would only be loaded once.

查看更多
放我归山
4楼-- · 2019-01-30 00:25

What has been most useful for me:

  • Build on a RAM filesystem. This is trivial on Linux. You may want to keep a copy of common header files (precompiled or the actual .h files) on the RAM filesystem as well.
  • Precompiled headers. I have one per (major) library (e.g. Boost, Qt, stdlib).
  • Declare instead of include classes where possible. This reduces dependencies, thus reduces the number of files which need to be recompiled when you change a header file.
  • Parallelize make. This usually helps on a case-by-case basis, but I have -j3 globally for make. Make sure your dependency graphs are correct in your Makefile, though, or you may have problems.
  • Use -O0 if you're not testing execution speed or code size (and your computer is fast enough for you not to care much about the (probably small) performance hit).
  • Compile each time you save. Some people don't like this, but it allows you to see errors early and can be done in the background, reducing the time you have to wait when you're done writing and ready to test.
查看更多
Animai°情兽
5楼-- · 2019-01-30 00:26

If you're doing a lot of recompilation, ccache might help. It doesn't actually speed up the compilation, but it will give you a cached result if you happen to do a useless recompilation for some reason. It might give an impression of tackling the wrong problem, but sometimes the rebuilding rules are so complicated that you actually do end up with the same compilation step during a new build.

Additional idea: if your code compiles with clang, use it instead. It's usually faster than gcc.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-30 00:29

This paper describes a method for compiling template code much like "traditional" non-template object files. Saves compile & link time, with only one line of code overhead per template instantiation.

查看更多
登录 后发表回答