5 ms·
On the performance dimension, I am pretty sure C++ effectively passed C a while ago given good compilers. At the very least, it requires significantly more code
by jandrewrogers 7y ago
On the performance dimension, I am pretty sure C++ effectively passed C a while ago given good compilers. At the very least, it requires significantly more code and effort to make C as fast as modern C++.
- totalperspectiv 7y agoGood point, I had always just taken for granted that C would be faster, and it's not! (Obvious disclaimers about benchmarks inserted here https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/cpp.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/...).
- igouy 7y ago> and it's not! Well those numbers seem to show some C programs are faster than the C++ programs they are compared to; and other programs are not.
- __d 7y agoI'll point out that those benchmarks show C++ faster for four, C faster for five, and both the same for one scenario. Modern C++ enables some very concise expression, at the cost of significant language complexity, and enormous compile-time cost. C requires more work from the programmer, but is vastly quicker to compile.
- flukus 7y ago> I am pretty sure C++ effectively passed C a while ago given good compilers Citation? Every benchmark I've ever seen still has C coming out on top: https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/c.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... . The gap has narrowed and c++ can be better in specific situations but it has not "effectively passed" C, not to mention uses less memory. Here is another one: https://greenlab.di.uminho.pt/wp-content/uploads/2017/10/sleFinal.pdf https://greenlab.di.uminho.pt/wp-content/uploads/2017/10/sle...
- pjmlp 7y agoIt is always a matter how good are those benchmarks written. For example, good luck achieving in C what is possible to do in C++ with constexpr at compile time, and will require an additional runtime cost during the benchmark execution. Or sorting data via qsort() versus STL sorting algoritms with templates, which in C always require the runtime cost of an additional indirect call. Or now with C++20 contracts, validation of pre-conditions, which with C are only possible at runtime, while C++ can already make use of the information if available at compile time. C++Now 2019: Odin Holmes “Hey C, This Is What Performance Looks like" https://www.youtube.com/watch?v=CNw6Cz8Cb68 https://www.youtube.com/watch?v=CNw6Cz8Cb68
- flukus 7y ago> It is always a matter how good are those benchmarks written. I agree it's not perfect, but I'm yet to see a single set of benchmarks show that "C++ effectively passed C a while ago" and yet I see it claimed somewhat frequently around here. Generally the games I see around benchmarks go the other way though, with people effectively writing C in the comparison language and not idiomatic code. > For example, good luck achieving in C what is possible to do in C++ with constexpr at compile time The only thing constexpr solves AFAIK is doing it all in c++, generating compile time constants is hardly magic and it's been solvable with anything from a shell script to something more complicated forever. > Or sorting data via qsort() versus STL sorting algoritms with templates, which in C always require the runtime cost of an additional indirect call. It's not as ergonomic, but things like this are also doable with _Generic in modern C.
- clappski 7y agoconstexpr is much more powerful than just generating constants (although that is one of the bigger use cases). One of the things I like is how it simplifies doing static dispatch on template types, essentially using SFINAE under the hood but in a much simpler way to read: if constexpr ( std::is_same_v<T, int> ) { ... } Vs template <typename T, typename std::enable_if_t<...>* = 0> void fn( T t ) { } Being able to generate constants using the language itself is super powerful as well (the below is a bit contrived, as std::string_view is constexpr and obsoletes this pattern): static constexpr std::array<char[], SIZE> strs = { ... }; static constexpr std::array<size_t, SIZE> str_lens = calc( strs ); // Where calc() loops over the array and calls std::strlen, all at compile time