How to override opCmp when two different sorts are needed?

janderson askme at me.com
Tue Aug 14 22:28:11 PDT 2007


C. Dunn wrote:
> Jarrett Billingsley Wrote:
>> Write your own sort routine which takes a given predicate.  :\
>>
>> Tango provides such a routine in tango.core.Array; phobos provides no such 
>> analogue.  With phobos, either write your own routine, or use 
>> std.c.stdlib.qsort.  I'll let you decide which is more elegant. 
> 
> It's not just a matter of elegance.  C++ std::sort() is way, way faster than qsort in cstdlib because the comparison function gets inlined.  Sorting is one place where inlining is critical.

In my experience/benchmarks (MS VC++ additions of C++ 2002/2003/2005) 
I've found that qsort is marginally faster at sorting then std::sort. 
Now it probably the type of data I was sorting however I don't support 
the way way faster argument.

Note the data I was sorting was pointers, 10-200 elements many times a 
frame.  Also note that std::sort will call the slow copy constructor if 
you use direct objects while qsort can't.

Also MS std::sort is much slower in debug due to all the extra checks 
they put into iterators.

-Joel



More information about the Digitalmars-d mailing list