4

My current project takes 8 seconds to build one C++ file. Is this common? Other files take 12-15 seconds but most at the moment are about 8 seconds. I use Visual Studio to tell me the time.

I have gone into the headers and removed things I didn't need and used forward declaration. It did save me about 2 seconds per file and one header takes about 4 seconds to go through alone. In that header I need all its declarations and using a lite version which removes private methods public functions I don't need.

Should I be happy it's 8 or so seconds or is that too long to compile one file?

  • 2
    What do you mean by “one file”? One compilation unit? Or actually one source file, without any headers etc.? Otherwise, it entirely depends on the headers (e.g. template-heavy headers such as the ones from Boost, especially Spirit), can easily take compilation time up to several minutes. – Konrad Rudolph Feb 13 '12 at 15:39
  • your project must be very small, since you want to optimize 8 second build – BЈовић Feb 13 '12 at 16:31
  • 1
    @Doc: Most of my questions are opinions and don't have answers. So I can not accept -edit- VS 2010, vector, map, iosfwd in one file and a large generated header which i use every class in – user2528 Feb 13 '12 at 17:26
  • @VJovic considering how many times i need to compile/syntax check this file. Its taking up more time than everything else – user2528 Feb 13 '12 at 18:02
8

C++ is a very complicated language, and due to many factors the compiler needs several passes to determine what a certain symbol is. The result is that C++ programs are slower to compile than programs of comparable complexity written in other languages. Here is an insightful article that covers the subject (it was written by Walter Bright, the creator of D and the author of the Digital Mars C and C++ compilers). Considering this, your 8 seconds compilation seems reasonable.

Good practices to increase compilation speed:

  • forward declaration whenever possible
  • precompiled headers
  • #pragma once (not standard, but both MSVC and GCC support it)
  • pimpl pattern
  • And not to forget: invest some money into faster hardware, a modern multicore CPU, enough RAM and a fast SSD can help a lot. – Doc Brown Sep 27 '13 at 15:04
3

If you're using a really old compiler, then it can be nasty- they don't support things like concurrent compilation. Else, I'd suggest that your files are way too large and you need to split them up. Incremental build is per-file, as far as I'm aware. I've got some files in my current project where the header is a thousand lines of template metamachinery (plus a bunch of Standard headers) and the contents of the source file are expression template magic to create a type so large, Visual Studio crashes half the time trying to compile it.

But when it succeeds, it still only takes a second or so.

3

I just did an eight second test and compiled 46 files in that amount of time. A lot of factors go into your compile time. It's not just the code itself, but your processor speed, hard disk speed, amount of RAM, whether the files were in cache or not, whether your code is on a network drive, what else is running on your computer, etc.

  • 1
    Assuming you want the compiler to go faster its a question of if the bottleneck is disk, memory or CPU. You can make any of them go faster but you need to figure out which one to focus on. – Zachary K Feb 13 '12 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy