Advertisement

Cutting Compile Times

Started by April 27, 2000 09:59 AM
24 comments, last by Kylotan 24 years, 8 months ago
The subject pretty much says it all. I use MSVC5.0, and my projects seem to take an age to compile compared to, say, other people''s projects written in C which I download and compile. Therefore I''m guessing it is either the C++ compiler or the C++ features I''m using which are slow. I use a lot of templates, mainly STL ones, which seem to be the main problem. But I can''t easily do without them. I only include the headers I need but it can still take 2 or 3 minutes to build a very simple project, and 20 minutes or more to build a more complex one. Does anyone have any general hints for speeding up compile times?
Get a faster computer. I use MSVC++ 5.0 and dont'' really have a hard time compiling stuff. I seem to use the same setting as default and it only takes seconds to compile a simple program, haven''t really tried to compile a big one with MSVC yet. I''ve compiled big programs with an older MS dos compiler and it still didnt'' take very long. WHat kind of speed do you have on your comp? If you''re using a 486DX that could be your problem. I have a K6-2 366 with 72megs of RAM just for reference. Cheers!
Advertisement

I agree, even on my p400/128mb ram it takes longer than it should to compile (msvc 6.0). But, I too, heavily use templates. This is the biggy I think, on slowing down the compile times.

If I have a straight WIN32 app, and no templates, it blazes through the compile.

You can use the pre-compiled header feature of MSVC to decrease compile times, but it isn''t any help with templates.

Z
__________________________________________

Yeah, sure... we are laughing WITH you ...
Yes,
templates, while being an amazingly useful tool, have a horrible impact on compile times. Unfortunately, I'm not really sure what to suggest. I use STL and other template libraries heavily, and I've just come to live with the pain. Although my next project may have to alter design because of the compile-time issue. We're planning on using expression templates and some other compile-time tricks to improve performance in a numerical simulation library... Unfortunately, we've seen statements like:

For example, with KAI, a simple addition of four vectorswith POOMA takes around 2 hours to compile with fulloptimisation on  a PII/366 Win2k. However the resulting coderuns as fast as FORTRAN or hand coded C. On the other handthe same code compiles in 2 minutes with no  optimisation,but runs 20x slower than FORTRAN/C because the expression template tree gets largely parsed at run-time. 


Obviously, this kind of tradeoff isn't one you want to have to make. (:

-Brian

Edited by - osmanb on 4/27/00 10:57:09 AM
How many templates do you guys use?
Ive made some of my own function and class templates for a program, and compile times are the same without them (which is only a couple of seconds).

Home made high quality PC- PII 350, 128MB Ram, MSVS 6.0 Enterprise Edition.

Compile times can be cut down considerably by using precompiled headers... stick all the STL stuff in a file called stdafx.h or whatever you want to call it... make a stdafx.cpp which only includes the file stdadx.h ... go to your project settings and setup your source files to use precompiled headers and stdafx.cpp to create a precompiled header thru stdafx.h ... I have run into a problem here or there with the compiler barfing on some of the STL.. so you may have to include some of it outside of being precompiled... if you are using any large libraries... make sure to precompile their headers too... basically anything that doesn''t change in your project can be precompiled... though, even if they do change... MSVC will detect it and recompile the precompiled stuff... I dunno if this makes sense... basically.. the biggest boost I ever got to compile times (40%+) was by figuring precompiled headers out...
Advertisement
I don''t really want to use precompiled headers. The reason being that the standard way of using them is to include the whole lot in each .CPP file, right? Usually by way of an intermediate header such as stdafx.h which MSVC often uses. Well, in my projects, I have to do a lot of editing of the header files, which would force recompilations of pretty much all my CPP files if I included those headers. And if I left my project header files out and only included standard library headers, I expect I wouldn''t gain much. I am not sure how much, if at all, the compiler can precompile templates. After all, they don''t really do anything until you instantiate them, which varies depending on the individual CPP file generally, and therefore would be compiled on a per-file basis anyway, no? It doesn''t sound like it would gain me very much. Any opinions on this?
If you''ve got enough memory you might consider doing your compiles on a project on a RAM disk. Alternately consider moving the contents of your lib and include directories to a the RAM drive. Or, even better, do both. You''d be surprised how much time compiliers spend doing disk access.

If you have to upgrade, in general, more RAM does more than a better processor. A faster hard drive couldn''t hurt either.

Alternately, if you use only a few STL classes, consider wrapping them in an new classes and putting the implementations in a separate C++ file. This will prevent it from inlining every darn thing at the runtime cost of actual function calls. Then again, it''ll probably save space in the final build.
quote: Original post by SiCrane

If you''ve got enough memory you might consider doing your compiles on a project on a RAM disk.


Nope, still on 48mb. Can''t afford more right now.

quote: Alternately, if you use only a few STL classes, consider wrapping them in an new classes and putting the implementations in a separate C++ file.


I use list extensively, vector a fair bit, and map and deque a little. But I would have no idea how to go about wrapping them in some way that would not break all my code and/or take forever. And would I not really have to either do another template class (thus reducing any benefits) or a separate instance of the container for each type (really not an option)? Any hints or pointers on this?

quote: This will prevent it from inlining every darn thing at the runtime cost of actual function calls. Then again, it''ll probably save space in the final build.


Yes, my biggest project has a 1mb executable, which I''d like to cut down.
In my stab at building a large lib (20+ modules), I set the precompiled header to stdafx.h, put all of my forward class declarations in it, then all the class header files. In each .cpp file, I put only #include "stdafx.h". When I had to change the header of a class (like what? 200 times a day ) and hit the build button, it would re-compile all of my .cpp files (again, 20+), but would run through them in about 2 or 3 seconds. Total. Templates, home-built collection classes, included. In fact, I probably spent more time rebuilding my LinkedList class more than everything else combined. All template classes were contained purely within the headers. Not bad for an AMD K6-2 450 with 64 MB of RAM.

Move #include "stdafx.h" line from the .cpp files to the header files of the modules, and just #include the class header files in the .cpp files, and you get instant performance...drop! It's slightly faster (about 1 second) when you change one header, and the change doesn't affect any other class, but otherwise the thing takes 2 or 3 seconds, per module! :/

I don't know if the experts would agree, but putting everything in stdafx.h and letting the compiler juggle which ones to compile first never seems to be worth the trouble of adding in like 2-5 includes for each module to track which classes are dependent on each other.

Perhaps I just ran into an odd combination of circumstances that don't happen in real life? For that particular project, I used windows.h, and nothing else.


Well, if the above is true in normal real life circumstances, then I'd make a rule with how to use header files:

    1) Place forward declarations of all classes (except templates, of course) in the stdafx.h.

    2) Include the stdafx.h in all .cpp files.

    3) In each class header, include only the necessary headers (base classes, library functions, classes used as data members, etc.) to ensure that particular header will compile fine on its own, in any order. (optional)

    4) Place the customary #ifdef/#endif compiler directives around the contents of each header.

    5) Place this line in each file, just inside of the compiler directives, but before anything else:

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

That's what I do, and I've never had a problem with compile time. Let me know if I'm wrong somewhere though.


- null_pointer
Sabre Multimedia


Edited by - null_pointer on 4/28/00 7:08:03 AM

This topic is closed to new replies.

Advertisement