News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Why does C++ take so long to compile?

Started by jj2007, May 27, 2024, 09:15:09 AM

Previous topic - Next topic

jj2007

Insights into compiler technology. Below an excerpt, the original is much longer :cool:

QuoteOK, so it's not #define lookups themselves, but it would be easier to create fast compilers if we didn't have them. It's not the complexity of C++ really, though that does exacerbate things. The heart of the problem is header files.

C also has header files, and is not nearly as slow, but that's because C header files are incredibly simple and generally short. C++ headers usually contain templates and class definitions (complex, big), whereas C headers are mostly just structs and function prototypes. That means C++ headers are not only much longer, they are also much harder to parse than C header files.

But that's only a piece of the puzzle. Any individual C++ header file only takes a small amount of time to parse. If we only had to parse each header file once, then compilation would be about five times (!) faster than it is. The real problem is that every header file has to be parsed many, many, many times. If you have 200 source (CPP) files, and each of them #includes MyHeader.h, then MyHeader.h is going to be compiled, from scratch, 200 times. Ouch.

Vortex

Hi Jochen,

What about the precompiled header files?

jj2007

Yes, that also came to my mind. The post doesn't mention them.

TimoVJL

QuoteMy experience is that precompiled headers speed up C++ project compilation 4-6 times, which means that header compilation alone was typically taking 75-85% of the grand total project compilation time. If you're suffering from C++ compile time problems, you owe it to yourself to move to precompiled headers. It's still not as fast as C, but it's like night and day compared to doing without them.
May the source be with you

greenozon

C++ modules (since C++20) will be the game changer...

https://en.cppreference.com/w/cpp/language/modules