4 ms·
Bjarne should know better than to directly compare the performance of std::sort and qsort. One is typically printed in full in a header file, while the other i
by comex 12y ago
Bjarne should know better than to directly compare the performance of std::sort and qsort. One is typically printed in full in a header file, while the other is typically compiled separately. If qsort were found in a header file, it could be inlined into identical code to the C++ version, regardless of the fact that there are void pointers lying around everywhere.
There are caveats: the compiler might not choose to inline unless you force it to, and if you do that then you'll end up with duplicate code in the case of multiple calls with the same comparison function, while C++ can automagically merge duplicates (although you probably want to write a wrapper function anyway, and C++ will still waste compile time generating the duplicates if the calls are in different source files). Also, if the sorting function calls a secondary function in multiple textual locations, and that function is significant enough that inlining it would produce wasteful code, the pure inlining-based approach will be insufficient (but I don't think most sorting algorithms do this).
In other words, C++ makes it easier to do this sort of thing. No surprise! It certainly makes it prettier. But when it comes to performance, in practice the above would likely not be a big deal for qsort, so the difference between the two functions is really more a matter of convention regarding the implementation location. Benchmarking the two and explaining only that type safety "makes for excellent inlining and good optimizations" is simply misleading.
- blub 12y agoAre you sure that the callback can be inlined though? How would that work, the calls through the pointer would be replaced with the function body? I would assume that qsort itself could be, but that wouldn't help much.
- comex 12y agoYes - modern compilers can replace function pointer calls with direct calls if the value is known, and from there they can be inlined.
- Aldo_MX 12y agoDoes compile time really matter that much? I mean, unless you are building a huge project which is expected to take hours to compile, I don't see much benefit in optimizing compile times.
- throwawayaway 12y agoi worked out the number of compilations i did for a year and the cost of c++ vs. c for a 15000 line project and i think it came to ten minutes total. which i found shocking, but there it was. my perception was different from the reality.
- Scea91 12y agoI am working on a project where full rebuild takes about an hour. Fortunately, we do not need to perform full rebuild often on our development machines so most of the time I can build with the changes I've made in between 10 secs to 5 minutes. I would expect this to be similar for other projects. Surely we care about build speed, but there is no need to care about such micro optimizations as qsort vs std::sort. Most of the speedup is in correctly separating translation units and not including unnecessary header files (sometimes forward declaration is enough for example...). With that said, when there are some tradeoffs then optimizing build speed is among one of the last things we care about.
- kbart 12y agoTo fully rebuild the whole project (U-Boot, Kernel, fs) I'm currently working takes 6 to 8 hours on 8 core i7 and believe me, I've done it may times.. So yes, compile time does matter sometimes.
- humanrebar 12y ago> Does compile time really matter that much? Yes. Which is why C++ needs modules. It's a big problem when people have to alter their designs to work around their tools (compilers, in this case).
- Aldo_MX 12y agoOf course compile time matters when features like modules provide a significant improvement. My question was more focused in having to work around the compiler by doing micro-optimizations, or avoiding some features that "may be heavy", just because one project will take 65535ms instead of 65000ms to do a minimal rebuild. But at the end of the day, it really depends on the dimensions of the project. In a huge project an improvement from 8 hours to 7.5 hours is greatly appreciated.