4 ms·
Russ argues that since you can compile lots of valid C programs as C++, surely C++ "can't lose" but this is a revealing error. It's true that lots of valid C p
by tialaramex 5y ago
Russ argues that since you can compile lots of valid C programs as C++, surely C++ "can't lose" but this is a revealing error.
It's true that lots of valid C programs are also valid C++ programs but it is not true that those programs will mean the same thing when treated as C++, nor does it mean that all C++ compilers are automatically just as good at optimising all possible valid programs as C compilers, and so the code which you get out and then measure may be quite different.
- laurent123456 5y agoI think he means that if you keep optimising a C++ program, or certain part of it, you eventually get to something very close to C, thus with similar energy and memory consumption. But indeed that's the problem with that kind of comparison - what's being compared exactly? How optimised is each program?
- loeg 5y agoNo, I think RSC means that C89 programs are literally valid C++ programs, and more recent C dialects nearly so.
- int_19h 5y agoBut C89 programs aren't literally valid C++ programs, either. Although I can't see any reason why those differences would affect performance. With newer C versions, there is "restrict", which can lead to better optimizations. And VLAs to avoid some heap allocations (albeit at the cost of potential stack overflow).
- qsort 5y agoIt is also not necessarily true that a C compiler will be better than a C++ compiler at optimizing the same code. Even more so considering that both gcc and clang are basically C++ compilers regardless. In most cases, giving the compiler more information to work with makes the resulting code faster. C++ is generally slightly faster than equivalent C, even though the difference is usually very small. I have to agree with the OP, C being 34% faster than C++ is simply an absurd result.
- truffdog 5y agoDon't clang, gcc, and msvc share a bunch of code between their C and C++ frontends (and uh, share the backend).
- jcranberry 5y agoC++ has more complexity not just more information. These comparisons are always flawed because they vary not just by language but by expertise of the one writing the benchmark in each language. And I think C++ has more potential for writing suboptimal code due to its complexity. It's also not obvious that all the different c++ constructs help the compiler speed up, IMO.
- igouy 5y ago> … vary not just by language but by expertise… When you wrote "always flawed" perhaps you meant inevitably flawed ? If not, then perhaps you could suggest a practical approach that would not exhibit that variation.
- igouy 5y agoSee https://news.ycombinator.com/item?id=30505343 https://news.ycombinator.com/item?id=30505343
- kevincox 5y agoI would be that compiler differences are trivial. Especially since all C compilers are also C++ compilers. You may just have tiny different language semantics that may optimize differently. I think what would matter more is that C++ encourages different patterns which may be less efficient. So instead of using an on-stack buffer you may use a std::vector or instead of a data-structure tuned to your application you may just use std::unordered_map. Of course this can bite both ways, the provided data-structure is likely more performance optimized than what you will write (most of the time) so if it is a good match for your use-case you may get better performance from C++. So I don't agree with "C++ can't be slower than C because that C is C++" because your average C++ isn't C. It is like saying "C can't be slower than asm because you can just embed the asm in your C program", it is true after infinite optimization time, but doesn't reflect how most of most C programs are written.
- jeffbee 5y agoThat's odd because in my circle it is generally recognized that C++ idioms are more efficient than C. Sometimes dramatically so. The canonical example of course is std::sort vs. qsort, where C++ wins big. But also anywhere dynamic allocation is called for, the efficiency gains of new/delete over malloc/free are going to become apparent.
- loeg 5y ago> the efficiency gains of new/delete over malloc/free are going to become apparent. What are those?
- jeffbee 5y agonew, when the compiler can see the definition at build time and with known size, may compile down to as little as two x86 instructions without a call. That's not generally true of malloc. C also lacks an equivalent of sized delete, which is very efficient. In C the implementation of free has to figure out what size ptr represented.
- 5y ago
- mattnewton 5y agoI took it to mean idiomatic C++ features make it slower than C, but now I am not so sure. I would like to see some example programs with that delta, it is surprising even if that was the reason.