6 ms·
> It has been slower than C++ ever since reasonable optimization was implemented. Which compilers? Which platforms? Optimized for speed or space (because "fast
by kabdib 7y ago
> It has been slower than C++ ever since reasonable optimization was implemented.
Which compilers? Which platforms? Optimized for speed or space (because "faster" is only one dimension of optimization)?
Many C / C++ compiler implementations share the same front ends, back ends and runtime libraries, so it's hard to see how the code generation for C would be much different than that of C++. (In fact, C++ will be harder to optimize if features like exceptions and RTTI are used).
Given the many different commercially and freely available toolchains, this statement is difficult to back up.
- mumblemumble 7y agoFor my part, I'm never interested in blanket comparisons of what language is faster, but it is worthwhile to mention specific features and how they impact performance. For example, in Python you can talk about the GIL and pointer chasing. But you can also talk about Cython. It turns out that, for me, Python's FFI is a really important factor, and a big reason why it's much easier for me to get good performance in Python than it is in Java: It makes it easy to interact with C libraries without taking a big hit on marshaling data in and out of Java's managed heap. (This is, incidentally, a major factor in Python having so thoroughly supplanted Java in the ML/BI/DS/analytics space.) But there are other situations where Java would be the clear winner. My favorite performance comparison between languages ever was the one that Raymond Chen and Rico Mariani did on implementing a practical program in C# and C++. [1] As it turns out that, at least as of 14 years ago, C++ is faster than C#, and C# is faster than C++, under different circumstances. You've really got to pay attention to which ones matter most to you before deciding where to place your bets, and just paying attention to rankings on The Computer Language Benchmarks Game is a sign that you've tried to jump to the answer without really taking enough time to understand the question. [1]: https://blogs.msdn.microsoft.com/ricom/2005/05/10/performance-quiz-6-chineseenglish-dictionary-reader/ https://blogs.msdn.microsoft.com/ricom/2005/05/10/performanc...
- igouy 7y ago> … just paying attention to rankings on The Computer Language Benchmarks Game is a sign that you've tried to jump to the answer without really taking enough time to understand the question. Instead, we could pay attention to program measurements: https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/spectralnorm.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... :and source code.
- Someone 7y agoThe one area where C++ can be a lot faster than C is in places where C uses a function pointer where C++ uses a function template. The standard example is sorting. Say you’re sorting an array of integers. Then, C’s sort has to (1) call the comparison function passed as an argument, whereas C++’s std::sort can inline the comparisons into a single instruction. (1) if the source of the function passed in is visible from the compilation unit, I think the C standard allows the compiler to compile a specialized version of sort in the same way C++ can, but I’m not aware of any compiler that does that
- kabdib 7y agoRight, inlines are hard to beat, and they are not part of the C standard (which is kind of mind boggling, I mean . . . honestly, what year is it?). Still, the benefit of inlines diminishes as the size of the inlined functions increases (you start paying penalties for extra cache lines full of code, and the percentage of time in function call overhead gets small in a hurry). The linker can get into the inlining game, too, by the way, if the win is sufficiently big. I have scars to prove it. :-)
- colonwqbang 7y agoWhat do you mean by "inlines are not part of the C standard"? The standard talks about visible behaviour of programs, never the concrete implementation.
- kabdib 7y agoYou're correct about C99. I cut my teeth on K&R; C99 has inline declarations.
- ncmncm 7y agos/The one area/One area/. Another is where a better algorithm is available in a library than would be convenient to implement in custom form in C. C++ libraries get a lot more attention to optimization than C libraries because it pays back much more. For example, there are really excellent hash table libraries in C++ that would be unimplementable, as a reusable library, in C. C hash tables are typically hand-rolled in place.
- ncmncm 7y agoThere is no such thing as a C / C++ compiler implementation. There are C implementations, and C++ implementations that may share some components. They do not typically share optimizers, because optimization of C++ requires quite different characteristics of an optimizer than one for C -- although most of the C optimizations may be applied after the C++ ones have run. If it is hard to see how C++ optimization would differ from C optimization, you simply aren't looking very hard. There are many different toolchains, but with the toolchains that are commonly used, C++ programs are faster than C programs. More specifically: C++ programs are at least as fast as C programs, because a C program is a badly-coded C++ program that fails to use language constructs that would make it a better program.