This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Limit std::sort comparisons on small list
- From: David Kastrup <dak at gnu dot org>
- To: libstdc++ at gcc dot gnu dot org
- Date: Fri, 04 Oct 2013 23:51:25 +0200
- Subject: Re: Limit std::sort comparisons on small list
- Authentication-results: sourceware.org; auth=none
- References: <52409F6F dot 7040609 at gmail dot com> <alpine dot DEB dot 2 dot 10 dot 1309232317070 dot 4088 at laptop-mg dot saclay dot inria dot fr> <alpine dot DEB dot 2 dot 10 dot 1309290031480 dot 4104 at laptop-mg dot saclay dot inria dot fr> <CA+jCFLvhxsbAu+3Ti07pL0hg7UJfDgBzzbAV5EMwvMUtaa1ZYQ at mail dot gmail dot com> <5249A299 dot 3060103 at oracle dot com> <CA+jCFLu-r8SCWhSv-h+vrcSbWXWAXEiFxVhfNTUpworkXMJsRw at mail dot gmail dot com> <524B322A dot 1090703 at gmail dot com> <C62520F9-5A70-406F-AAE8-0F7900034CC3 at oracle dot com> <524DCDC7 dot 1000801 at gmail dot com> <524DD93A dot 30100 at oracle dot com> <524DDACB dot 7010207 at oracle dot com> <524F2892 dot 3040204 at gmail dot com>
François Dumont <frs.dumont@gmail.com> writes:
> On 10/03/2013 10:59 PM, Paolo Carlini wrote:
>> .. or, if I remember correctly, should we maybe see what happens for
>> a smaller or much smaller number of elements to see if there is a
>> measurable (in number of comparisons terms), on sort?
>>
>> Thanks,
>> Paolo.
>>
> I run the sort performance test using only 11 elements and in this
> case I see:
>
> Without patch:
> sort.cc reverse 10 comparisons 0r 0u
> 0s 0mem 0pf
> sort.cc forwards 20 comparisons 0r 0u
> 0s 0mem 0pf
> sort.cc random 30 comparisons 0r 0u
> 0s 0mem 0pf
>
> With patch:
> sort.cc reverse 10 comparisons 0r 0u
> 0s 0mem 0pf
> sort.cc forwards 19 comparisons 0r 0u
> 0s 0mem 0pf
> sort.cc random 29 comparisons 0r 0u
> 0s 0mem 0pf
>
> so with a limited number of elements we can see the gain even if it
> looks like for the sort algo it is limited to 1 comparison. For
> stable_sort it is better.
The theoretic optimum is lg(n!), namely about 26 comparisons. With any
efficient sorting algorithm, sorting small sublists with something
primitive like insertion sort does _not_ have the purpose to lower the
worst case or average number of comparisons but rather reduce the
administrative overhead for enough comparisons that you gain speed in
_spite_ of slightly _increasing_ the number of comparisons.
So either your numbers here are snake oil caused by random or systematic
flukes, or the implementation of the efficient part of the algorithm is
not overly efficient.
Now if we are talking about an unmodified standard quicksort (not! the
modified median of three), the best, average, and worst case behavior
numbers for comparisons are 22, 28.5, and 55. Now your "random" case is
already worse than the average for an unmodified qsort, and the
unmodified qsort is worse than the usually applied median-of-three
qsort.
The really, _really_ suspicious thing however is your best case behavior
of 10 comparison either way. The only way to get that is to compare
each not-yet viewed element to either the largest or the smallest number
in the sorted set and thus increase the size of the sorted set by 1 with
each comparison. But that means a comparison scheme/order that is not
commensurable with an efficient sorting scheme since it chooses
comparisons that produce significantly less than 1 bit of information as
the probability of having a number that is at the correct end of the
already known range drops quite below 0.5 after the first few tries.
Even the best case for a mergesort (which has a non-degenerate worst
case of 29 comparisons) is 17 comparisons. 10 is way too low to make
sense for efficient sorts.
What's the mean number of comparisons for insertion sort? For a list of
mi(2) = 1
mi(n+1) = mi(n) + (1/(n+1)) (sum(i=1, n+1, i) - 1)
= mi(n) + n/2 + 1 - (1/(n+1))
So mi(11) is about 11 + 55/2 - ln(11) = 35. Ok, calculating exactly
delivers about 35.5 comparisons. Pretty good estimate.
But that means that with just 11 numbers, you are on pretty shaky ice
regarding detecting an insertion-sort quality contender among more
efficient variants.
Better use numbers like 10000 or so. That's still small, but quite less
so.
--
David Kastrup