3 ms·
The poster child for C++ being faster than C is std::sort() vs qsort(). The latter requires a function pointer dereference for each call to the user-supplied or
by zabzonk 6y ago
The poster child for C++ being faster than C is std::sort() vs qsort(). The latter requires a function pointer dereference for each call to the user-supplied ordering function - the former does not. There are many similar opportunities for C++ to be faster than C.
- flohofwoe 6y agoThe solution for C would be to "stamp out" a specialization of qsort() for a specific data structure either manually or with code generation. The C++ template system really isn't some "magic performance pixie dust", it just saves some typing ;) ...or alternatively, with global optimization (LTO/LTCG) there's actually a good chance that the comparision function calls of a C qsort() implementation can be "dissolved" via aggressive inlining as long as all required parts are visible to the compiler - that requires a lot of trust in the compiler though (on the other hand, C++'s "zero cost abstraction" philosophy requires the same level of trust).
- zabzonk 6y agoMuch of what you say is true, but I'm not sure that this is: > (on the other hand, C++'s "zero cost abstraction" philosophy requires the same level of trust). The C++ Standard _requires_ that a Standard Library implementation implements certain performance guarantees - I don't think the C Standard has such requirements. This means that certain implementations are effectively required - for instance that std::map is implemented (for better or worse) as a red-black tree.
- gpderetta 6y agoTo be clear, the standard mandates the complexity of most standard library functions. In no way it mandates zero copy abstraction (and it is not clear how one would even word that). Features are usually defined in such a way that zero cost abstractions are at least known to be possible though.