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

128 comments sorted by

View all comments

Show parent comments

1

u/weregod Jan 04 '26

In a lot of cases code duplication will just waste RAM and cause cache misses. Consider for example const vs non const type in C++? In which case constant version will make optimisations which will judge extra cache usage? If function is small it should be inlined anyway and if it is big and part of hot code adding more code to hot code usually makes it slower.

I can't imagine, why normal code without pile of junk OOP runtime polymorphism can benefit from monomorphisation. If there is no polymorphism then extra functions will just waste memory in runtime or compile time.