Speed Up Your Compilation Times
Tuesday, October 23rd, 2007For those who may have missed a couple of these, here is an index to the Speed Up Your Compilation Times series:
For those who may have missed a couple of these, here is an index to the Speed Up Your Compilation Times series:
Most of us judge hard drives almost exclusively on the basis of capacity. While this is fine for most people if you develop a lot you should know that the hard drive’s cache size and spindle speed can have a tremendous effect on you compilation times.
For example, I recently upgraded to a 10,000 rpm Western Digital Raptor (Raptor X
: same hard drive but has a window and is cheaper right now. Be forewarned it is louder than the non-windowed version) hard drive and compared to every other hardware change I’ve ever made this has had the biggest impact on compilation times. Something to consider the next time you build/buy a computer
This is one of those projects that is just so damn cool that even if you don’t use it you should still know about it. Have a bunch of machines around you? Want faster compile times? Use distcc. As you might gather from the name, distcc allows you to do distributed builds using gcc.
Essentially you install and start distcc on each machine, tell distcc on the main machine which machines to use, and run your build with distcc. The magic that is distcc takes care of the rest. I know it sounds too simple for something that seems so complicated but that’s the beauty of distcc: it really is that simple. Now go, and build things quickly.
In my last post I presented a technique for speeding up compilation times in a very limited number of a cases. The obvious question is then what about the rest of the time? Enter ccache. ccache is a compiler cache which is to say that if you request it to compile a file with the exact same preprocessor directives and compiler flags then the result from the cache will be used. I use this tool everyday and I highly recommend it.
For the nervous among you who are worried that ccache will produce results inconsistent with those from just using the compiler, I present this choice quote from the ccache site:
“The most important aspect of a compiler cache is to always produce exactly the same output that the real compiler would produce.”
If my recommendation and the assurances of the ccache site aren’t enough to convince you, I suggest you test it for yourself. I am positive you’ll find it works as advertised and that the decrease in compilation times will convince you to continue to use it.
Here’s a neat little trick that I’m not sure where I picked up. If you have to rebuild an entire project and it is fairly large give this a shot. Create one implementation file (.cpp) that includes all the other implementation files in the project like so:
#include "implementation_file1.cpp"
#include "implementation_file2.cpp"
...
#include "implementation_fileN.cpp"
And compile that file. This forces the entire project into one compilation unit and usually results in a fairly significant drop in compilation times (at least with gcc/g++). Be aware however that when a just a few files have changed then doing a partial rebuild with a build management tool like make is still faster. Though if you are doing a full rebuild then this trick is much faster.