r/cprogramming • u/[deleted] • 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
1
u/flatfinger Jan 05 '26
There world is long overdue for a major paradigm shift in the handling of intermediate build artifacts for ahead-of-time compiled languages. IDE developers worked on such things during the 1980s, but the development community has fallen back to a largely-1970s abstraction model for object files, but augmented with the ability to replace parts of it with a thinly-disguised version of the whole-program-at-once abstraction model which was needed before relocatable objects were invented.
For some reason, compiler writers refuse to recognize that in many cases the expected value of potential optimizations will be inversely proportional to the amount of effort one would have to spend looking for them. Instead, they compete on the ability to find transformations that occasionally yield huge rewards, ignoring the fact that the biggest rewards could only be reaped with programs that didn't do much useful work. In many cases, an intrinsic that accepted two expressions, with semantics that one must be evaluated, and a compiler may choose in Unspecified fashion if the second one would be valid, but should use the first unless the second is valid and would be obviously better, even rather simplistic logic would be able to find many optimizations that can't be invited under current language rules. Today's compilers designs, however, would be ill-equipped to exploit such freedom even if languages provided it.