6 ms·
> C++ doesn't take longer to compile if you don't abuse templates. Surprisingly, this is not true. I've written a C++ file only to realize at the end that I di
by petters 7mo ago
> C++ doesn't take longer to compile if you don't abuse templates.
Surprisingly, this is not true. I've written a C++ file only to realize at the end that I did not use any C++ features. Renaming the file to .c halved the compilation time.
- levodelellis 7mo agoI don't believe you, I measured compile times in c compilers and my own. If you provide more information I'd be more likely to believe you
- petters 7mo agoThat's fair. I'm unable to provide more information though so we'll have to disagree.
- flohofwoe 7mo agoOn some compiler toolchains (IIRC MSVC was the main offender) you get a lot more code pulled into your source file when including a C stdlib header (like <stdio.h>) in C++ mode versus C mode. Basically a couple hundred lines in C mode versus thousands of lines in C++ mode.
- levodelellis 7mo ago[flagged]
- jsheard 7mo agoI agree it shouldn't really matter if there's no C++ features in play, but I suppose third party headers could bite you if they use #ifdef __cplusplus to guard optional C++ extensions on top of their basic C interface. In that case the compiler could be dealing with dramatically more complex code when you build in C++ mode.
- uecker 7mo agoMaybe it is similar for the same compiler (but one should check, I suspect C could still be faster), but then there are much more C compilers. For example, TCC is a lot faster than GCC.
- levodelellis 7mo agotcc is 8x faster, twice as fast isn't doing it justice. As for the header thing, that'd could potentially be true if the compile time was something like 450ms -> 220ms, but why bother saying it when you're only saving a few hundred milliseconds
- petters 7mo agoGoing from 220 to 450 ms would be a disaster in my project. It has many thousands of files. Recompilation of almost everything happens from time to time. If those made-up numbers were true, they would be very significant and an argument in favor of keeping the code in C
- levodelellis 7mo agoA 200ms difference is adding or removing 200lines lines of implementation, and spliting it up into a file can make it slower because of include overhead. You completely made up C being twice as fast as C++.
- pjmlp 7mo agoThe question is the performance optimisations on top. 1990's compilers were also super fast, they only did optimisation for size, speed, constant propagation, and little else. Zero code motion, loop unroling, code elision, heap via stack replacement, inlining,...
- uecker 7mo agoOf course, but gcc with -O0 is still slower and there is no TCC for C++.