4 ms·
I would be that compiler differences are trivial. Especially since all C compilers are also C++ compilers. You may just have tiny different language semantics t
by kevincox 5y ago
I 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.
- remexre 5y agoIs that some sort of allocation elision where it's actually on the stack? Or more to the point, what are those two instructions?
- mariusor 5y ago> new, [...] may compile down to as little as two x86 instructions Yes, but when you have a million of allocation, that still means two million instructions. malloc/free allows a much easier path to allocating a large chunk of memory in which you can play around with your million structs having only the overhead of accessing pointers (and, of course, thinking about where to point them).
- kaba0 5y agoWith all due respect, that is trivial to do in C++, hell it will be actually readable and much safer than malloc(n*sizeof(something)) and then for looping over that to initialize values.
- maximilianburke 5y agoI mean, there shouldn't be any, given how many C++ standard libraries implement `operator new` on top of malloc. If anything it should be less efficient once you factor in constructor calls. libc++: https://github.com/llvm-mirror/libcxx/blob/master/src/new.cpp https://github.com/llvm-mirror/libcxx/blob/master/src/new.cp... libstdc++: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/new_op.cc https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-...
- jeffbee 5y agoIt is common in C++ to override operators new and delete. When you use tcmalloc in a C++ program it overrides all the global allocator and deallocator operators. When you use tcmalloc in a C program, it just overrides malloc, because there is no richer API available.
- maximilianburke 5y agoAnd the same overriding can be done in a C program with malloc and free, too.
- mariusor 5y ago> the efficiency gains of new/delete over malloc/free are going to become apparent. I think that you overlook how the model of new/delete encourages thinking about memory in individual chunks (of the size of your class) instead of a small number of large chunks that can be split into individual pieces as needed, but make the overhead of allocating/deallocating memory much, much lower.
- zozbot234 5y agoC++ has placement new and custom allocators for this. Rust actually has a harder time doing these things, though local allocators are in the works for Rust too.
- mariusor 5y agoMy C++ is very rusty (pun not intended), I had no idea you can plug in a custom allocator but still use new/delete for initialization. Do you maybe have a link to something where I can read more about this?
- tialaramex 5y agoThe C++ new keyword is just an operator and so you can overload it like most C++ operators, to do whatever you want. In fact it's not just one operator, it's a whole family of them.
- jeffbee 5y agoIn my opinion arena allocation is much more idiomatic, and more commonly seen, in C++ than it is in C.