r/cprogramming • • Dec 31 '25
Why does c compile faster than cpp?
I've read in some places that one of the reasons is the templates or something like that, but if that's the problem, why did they implement it? Like, C doesn't have that and allows the same level of optimization, it just depends on the user. If these things harm compilation in C++, why are they still part of the language?Shouldn't Cpp be a better version of C or something? I programmed in C++ for a while and then switched to C, this question came to my mind the other day.
26
Upvotes
29
u/madman1969 Dec 31 '25
The C23 standard is about 600 pages. The C++23 standard is 2000 pages. That should given you a clue as to the difference in language complexity between the two.
39
u/Comprehensive_Mud803 Dec 31 '25
It’s a different language, with less rules than bloaty C++, smaller libraries and thus less code to compile overall.
It’s a pure joy to work with C when having worked on a C++ project.
8
u/sreekotay Dec 31 '25
The type system by design can require significantly more complex matching and/or result in code expansion/explosion
8
u/Leverkaas2516 Jan 01 '26
C is much simpler than C++.
It's a more powerful language, and lets the programmer think about the problem in different ways. C++ makes it easier to build and maintain very large, complex programs.
They don't harm compilation. Both C and C++ can be compiled just fine. If you're worried about compilation speed, you're worried about the wrong thing. It rarely matters, and if it matters, you can tailor your build so that it doesn't.
Teams that use C++ instead of C do so because it's better for their purposes.
6
u/[deleted] Dec 31 '25
The problem is that c++ is a strictly more powerful language than c. C is a high level assembler, but c++ provides additional features (templates, compile-time execution, overloading and overload resolution). Those features all have a cost. In many cases, that means the compiler has to work harder: instantiating templates, executing constexpr and friends, selecting the right overload based on matching parameter types. This cost is also present in the more complicated grammar for c++, which causes the compiler parsing step to be slower and require more resources (again, slowdown).
This extra compile time work leads to a lot of the issues you see with c++ developer experience. Some say that this is a fine tradeoff for the extra power that cpp gives you (to an extent, I agree), but others rightfully complain that taking 10x time to parse iostream v.s. stdio.h is a big sore spot with the language (no, modules and precompiled header have not fixed this, and likely wont for as long as the long tail of legacy code exists).
Up to you as the developer to choose whether the eegonomics of the toolchain are worth dealing with for your choswn project.
2
u/WoodenLynx8342 Dec 31 '25
C++ has quite a bit of bloat. But it depends obviously. If you have some project in C++ that went ham on templates, it can get kinda silly with build times. But for the most part, what I've worked on, build times for C++ haven't really bothered me. But I also haven't worked on any massive projects either.
2
u/Tcshaw91 Dec 31 '25
I feel like the push to have the compiler do everything every build is a mistake. The compiler should just build the program and it should be fast. Static analyzers should be used to verify memory patterns and other things. Codegen like for generics, should be a separate pass imo. I think if those 3 things got separated and run at different intervals, it'd be a diff story.
Like if I have an Array<T> and the codegen pass generates a bunch of type specific definitions in a file, and I don't add any new types or change the definition, I shouldn't have to pay for that code gen every single build. I don't even mind a 40min wait time if it's done once at the end of the day or on lunch break, but that should be a full codegen and static analysis and tests and stuff. Not building and compiling an exe lol.
3
u/ffd9k Dec 31 '25
C++ is a tragic failure.
It was meant to be a better C with useful features added, and the slightly longer compile times were thought to be less of an issue as computers get faster.
But the added features brought more problems that were attempted to be fixed with even more features; templates were abused to fix some of these problems in ways they were never meant to be used, at the cost of outrageous compile times. c++20 modules were added in an attempt to keep these under control but this didn't work well.
Now C++ is a horrible mess. Most productive users use only some subset of it, nobody uses c++20 modules, and people have to resort to tricks like precompiled headers and unity builds to reduce compile times.
Unfortunately a lot of existing codebases and frameworks still use C++, because they were started at a time when people were hopeful that C++ would eventually get better.
I think for new projects it's best to use mostly C, with only the parts of the project in C++ where you need to interface legacy C++ libraries, especially GUI frameworks. Fortunately the compatibility between C and C++, originally meant for using old C code in new C++ projects, also works the other way around.
3
u/onlyonequickquestion Dec 31 '25
Templates replace developers time with compilation time. What do you value more, your time or your CPU time?
1
u/Cautious-Necessary61 Dec 31 '25
Is it appropriate to distinguish C from C++? I like to think C is a system language and C++ is application language. I’m intentionally not saying cpp is a superset of C.
1
u/Plastic_Fig9225 Dec 31 '25
I recently had the opportunity to enjoy a mixed C/C++ code base. And I was a little surprised to find that gcc was somewhat worse at optimizing compared to g++. At least for gcc it seems that g++ does more/better optimizations, which would of course also impact build times.
1
u/quickscopesheep Dec 31 '25
My guess is that the parser for c is much more complex. Even if you are not using the language features provided by c++ it still has a vastly more complex parsing algorithm in order to check for them. There are c compilers written in a few thousand lines if that.
1
u/exajam Dec 31 '25
C++ has more things, doesn't mean it's a better language. Most C programmmer would rather say the opposite actually
1
u/ElevatorGuy85 Jan 01 '26
By design C++ is a more complex language than C. The original C++ compiler front ends used to convert from C++ source into C source and then compile it using a C compiler.
See https://en.wikipedia.org/wiki/Cfront if you care to know more.
On top of that, C++ tends to have far more files being accessed during compilation, especially things like headers when you are using any sort of C++ framework/library. That’s not to say that C doesn’t have these too, but C++ often tends to have more and they tend to be larger files with many more inter-dependencies.
So now you have two factors - i.e. being “compute bound” (parsing the language and turning it into an object file) and also being “I/O bound” (reading source files as well as the header files, or at the very least finding the files first to check modifications dates), and more files in more directories will add to the total disk access time needed.
On the compute side of things, you have ever-more complex operating systems, virtual memory, RAM availability (or not) that is shared with other foreground and background processes and drivers. So with C++ compilers also being more complex, it uses more of these resources than C does. Some compilers like those in GNU’s Compiler Collection (GCC) consist of a front-end, a middle-end and a back-end, so more complexity and compute are needed for compilation, optimization and final machine code generation, and with C++ this will be more than C.
On the I/O side of things, there are now language-level strategies such as C++’s use of Precompiled Headers (PCH) - see https://en.wikipedia.org/wiki/Precompiled_header if you want to know more!
There’s also operating system techniques such as disk caching which can speed up access when reading and writing (the exact amount will vary based on workload, cache size, etc.)
Working against this you have things like modern anti-virus software that sits in the background and can slow down I/O speeds, as well as things like Microsoft’s OneDrive that can affect performance.
You might think that C++ is “slow”, but I remember the days of DOS C compilers on early IBM PC hardware where it would take minutes to compile the classic “Hello World!” Code. So “slow” is definitely relative!
I’ve also seen a custom Yocto Linux image build on a 12 core machine with 64GB of RAM that took about 70 minutes to rebuild. There was a LOT of C and C++ code in that just to build the OS and then the application that had many dependencies including the C++-based Qt framework. With some tuning to maximize compute and I/O resource utilization, e.g. by adjusting the number of parallel build processes, it was possible to improve build times by about 20%.
1
u/zogrodea Jan 01 '26
C++ does give some additional power for concise code that C itself lacks.
One example is "generics" like std::vector<T>; this is done through monomorphisation, which means that at compile time, one version of a "generic" function is generated for each type it is used with.
C lacks generics entirely, so it doesn't have to pay the compile time cost for them, but if you want something similar, you have to code a new version of a data structure or struct for each type you want to use it with. You have to perform the monomorphisation manually by hand, in other words.
That's a lot more code you have to maintain over time, and you also have the additional maintenance burden of having to change each version when one version changes. It's easier and less error-prone to let the compiler handle that.
It's the same with some other features with C++ too, like templates, as you mentioned. They relieve you of your maintenance burden by letting your code be more concise, but you pay for that in terms of compile time.
1
u/imaami Jan 01 '26
Cpp is the C preprocessor.
1
u/Ashamed-Subject-8573 Jan 01 '26
I just ported about 10 emulator cores from c to cpp and even without using templates and most standard library features (aside from replacing some of my c vectors with std vectors) and it went from under a minute compile time for 200+ files to under 3 minutes.
Make of that anecdote what you will
1
u/schteppe Jan 01 '26
If you use clang, you can profile your compilations using
-ftime-trace. Compare the flame charts for your cpp vs c files and see what the compiler is doing differently. This should be a great learning experience!1
u/ChadiusTheMighty Jan 02 '26 edited Jan 02 '26
Because it's simpler to parse and verify semantic correcness.
1
u/yvrelna Jan 02 '26
Slower compilation time means it takes 2 seconds instead of 1 second for the computer to finish compiling.
The more expressive language means it takes a 3 hours job into a 1 hour job.
Guess which optimisations most people cared about.
1
u/alex_sakuta Jan 02 '26
1
u/J-ky Jan 04 '26
Wait, are you serious?
1
u/h3llll Jan 04 '26
C++ is made(or at least tried) to help humans
C is made to help machines
see we see the world differently, of course, we see a dog and we thing that's an animal, we classify things.
machines don't understand that and we have to emulate it, and emulating a human concept on smart rocks is hard, and making it fast is even harder, even harder on the compiler and processor that's hosting the compiler, so both have to be complex and they'll both be hindered for the result we want.
there's a triangle that every software falls somewhere into between performance, usability and quality, C++ sits so near performance and usability(or it tries to, i'll explain it) that quality is barely visible, and it's not even all that usable it wants to be so performant it loses quality AND usability.
C sits so damn near the middle it's almost perfect, it leans more towards performance in trade of usability, obviously but it's generally a well designed language, it has been designed to run everywhere well so obviously it really does well on quality's side.
C++ is a mess because everyone tries too much to make it fast and usable they forgot about quality, now it's a mess, used just because it has been used before for no real reason.
1
u/SmokeMuch7356 Dec 31 '25
Because they are incredibly useful, allowing you to write type-agnostic code without resorting to gross hacks like
voidpointers and callbacks or macros out the wazoo.The tradeoff is a little extra build time, which no end user cares about, nor should you. If it's the difference between 20 and 25 seconds, wah. Shit, if it's the difference between 20 and 25 minutes, wah. I'm currently managing a code base that takes six hours (yes, hours) to build from scratch, and templates aren't the problem there.
The standard containers are all templatized; you can create
vectors ofint,double,struct foo,class Bar, whatever, and they all behave the same way. Same withmaps,queues, etc. What takes me half a day to write in C takes me maybe a couple of minutes in C++.C++ is chock full (maybe a little too chock full) of useful features that make some types of development much quicker and safer, but there's no such thing as a free lunch. It's a much bigger language with significantly more complex semantics than C, and that's reflected in build times for code that has to do a lot of type deduction.
It's just a matter of what tools you find useful.
1
u/psicodelico6 Jan 01 '26
C doesnt have generic
-1
u/Plastic_Fig9225 Dec 31 '25
Speed of compilation is way, way down on the list of common criteria for the "quality" of a programming language.
If you disagree, then you may want to look into assembly programming to avoid the massive blow the C compiler does to your build times.
0
u/Dusty_Coder Jan 01 '26
cpp is c with name mangling, taken to its conclusion
-1
u/Possible_Cow169 Dec 31 '25 edited Dec 31 '25
“You know how you only use 10 percent of your brain? That’s because the other 90 percent is filled with curds and whey.” - Todd from Scott Pilgrim Vs the World
1
u/CommunicationFit3471 Jan 04 '26
less bloated standard library and less language features to analise
0
u/[deleted] Dec 31 '25
[deleted] — view deleted comment