3 ms·
>STD::sort is closer to writing your own sort and putting in the function calls than calling an external function No it's not, that's like saying that using in
by moonchrome 15y ago
>STD::sort is closer to writing your own sort and putting in the function calls than calling an external function
No it's not, that's like saying that using inline operators for vector addition is the same as manually inlineing the code or that manually unrolling a loop is the same as the compiler doing it - by that logic everything is expanded to assembly therefore you should write it in hand optimized assembly to get a "decent comparison" (or LLVM if you want portability) and conclude that assembly is "faster" than C/C++.
Of course there is a reason C++ is faster - it's not magic - it compiles and runs on same hardware - but the point is that it has better abstractions that deliver more for similar code - C provides weak abstractions that don't make this sort of optimization easy for the compiler, so unless you like manually rolling out a sort function every time you need to use it it's actually slower than C++. Same goes for collection libraries, small vectors (eg. 3d vectors), etc. And it's worth pointing out because a lot of people assume low level == fast.
- veyron 15y agoFTA: By inlining C++ is able to run just the raw algorithm. On the opposite the C code has a number of indirections That is a reflection of using qsort rather than rolling your own sort. You could write your own macro that replicates what std::sort does and see better performance in C. That same abstraction can be built in C. If you want to argue that the C++ standard library has more utilities to simplify these optimizations, certainly I will agree. I think, however, that the statement that "sorting in C++ is 3 times faster than C" is an unsubstantiated leap.
- moonchrome 15y agoBy rolling your own I meant manually unroling a sort for each type instance you use it for. Sure you can use a macro but that's just nasty. Anyway I don't think anyone thinks C++ is magic but I guess the title could be cleaner "sorting with standard libraries C++ 3 times faster".