Friday, 26 February 2010

Increasing Code Compiling and Linking Speed

(http://imgs.xkcd.com/comics/compiling.png)

Over the last few days I've been working on increasing my code compiling and linking speed. A few of my files were taking about three minutes to compile. In addition, they depended on each other so any changes I made led to long, long periods of waiting.

Total compiling time was about fifteen minutes with about a minute or two of linking time. This was ridiculous but I had been putting off doing anything because the code started off being quite quick to compile and had only been increasing in small increments.

The first thing I did was look for a way to time how long everything was taking and seeing whether I could figure out where to focus my efforts.

I found an article that talked about using the Linux command `time'. This was quite handy because my Makefile compiles each file individually, all I had to do was change from my normal command to:

time g++ -c filename.cpp
time g++ -l filename.o


That gave me individual compilining and linking times. To get the total building time, I changed from my normal command to:

time make

I then went looking for ways to increase my compiling speed. I had a look at an article from EventHelix.

  • The main tip was to move header file inclusion code from header files to implementation files wherever possible.

I started with the file that took the longest to compile and proceeded to move all the header file inclusion code to the implementation files. Instead of having object member variables, I changed to having pointer member variables. This has made a huge difference. I went from a three minute compile time down to about six seconds. My overall compile time went from about fifteen minutes to about one minute.

Another source of long compile times was having lots of template code in header files (due to using Boost Serialization) and having lots of header file inclusion code in header files.

Moving header file inclusion code to header files was my idea and so was having template serialization code in header files. I've had to spend a lot of time fixing this up. Now I'm hoping that going to the other extreme won't come back to bite me.

No comments:

Post a Comment