[m-dev.] For review: additions to array.m
Ralph Becket
rbeck at microsoft.com
Fri Feb 2 01:16:46 AEDT 2001
>From Fergus Henderson on 01/02/2001 06:16:57
> On 31-Jan-2001, Ralph Becket <rbeck at microsoft.com> wrote:
> > Array based quicksorting is substantially less costly in
> > terms of space(O(n) vs O(n log n)) and informal testing
> > shows it to be about 10 times faster in practice.
>
> That sounds very good.
It also turns out to be wrong :(
> How do they compare when sorting already-sorted lists?
Tests carried out on larger lists (10000 and 100000 members)
showed quicksort to be 3-4 times more costly than mergesort
for ordered lists (ascending and descending) and 3-5 times
better on randomized lists (as expected). [See attached
file TestResults-NoRandomization].
So I altered array__sort so that it first randomly permuted
the array before quicksorting it. This meant that the
median-of-three pivot selection could be done away with.
The results after this change were much more encouraging,
showing randomized quicksort to be pretty much consistently
2-5 times better on all inputs. [See attached file
TestResults-WithRandomization].
As a side effect, we also get a potentially useful
array__permutation predicate.
> It would be nice to also provide versions which are
> parameterized on an ordering predicate, like list__sort/3.
Okay, I'll add those.
> > +:- pragma type_spec(array__sort/1, T = int).
> > +:- pragma type_spec(array__sort/1, T = string).
>
> Those are implementation details, really;
> I don't think they should go in the interface.
Fair enough - done.
>
> Why did you choose to specialize those types?
> You should document the reason.
Eariler testing showed that type specialization netted about a
30% improvement and it seemed to me that ints and strings are
the sorts of things that one wants to sort more often than any
other.
It also makes benchmark results look better when compared against
other languages :)
> Hmm, it might be nice to also provide
>
> :- mode array__foldl(func(in, di) = uo is det, in, di) = uo is det.
Done. I'll post a complete diff again once I've added in the
parameterized sort functions.
--
Ralph Becket | MSR Cambridge | rbeck at microsoft.com
begin 600 TestResults-NoRandomization
M"G-T87)T:6YG('-O<G1I;F<@8F5N8VAM87)K(&]N(#$P(&ET96US"F%V97)A
M9VEN9R!O=F5R(#$P(&ET97)A=&EO;G,*"FQI<W1?7W-O<G0O,2!O;B!A<V-E
M;F1I;F<@<V5Q=65N8V4Z(#!M<PIL:7-T7U]S;W)T+S$@;VX at 9&5S8V5N9&EN
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------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list