3 ms·
This example highlights the reasons to avoid function pointers when possible. An apples to apples comparison would use a handwritten sort routine for C instead
by veyron 15y ago
This example highlights the reasons to avoid function pointers when possible.
An apples to apples comparison would use a handwritten sort routine for C instead of using a library function: STD::sort is closer to writing your own sort and putting in the function calls than calling an external function.
- 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".
- crusso 15y agoI don't agree with how you define "apples". I would define an apples to apples comparison as "How would a typical C developer write this test? How would a typical C++ developer write this test?" A typical C developer would use a library function. A typical C++ developer would use the stl. This was a great article for me because I am actually about to write some very performance intensive data manipulation code that will use sorting and other similar algorithms. I was thinking about doing it in C because I always mentally associate C with performance. This article is a reminder to me that although I could eventually laboriously hand-tweak the C to be performant, there are things in C++ that will perform better than C without my even having to do much optimization, if any.
- 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 The typical C developer is aware of the deficiencies and would write a custom macro to do the sort (I have a whole macro library for many small functions) I agree that the language provides convenience, but the statement that sorting as a concept is faster in C++ than in C seems to be a stretch.