Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Choosing the right sort (Or: WUDsamatter?)

by ikegami (Pope)
on Apr 14, 2005 at 04:58 UTC ( #447649=note: print w/ replies, xml ) Need Help??


in reply to Re: Choosing the right sort (Or: WUDsamatter?)
in thread Choosing the right sort (Or: WUDsamatter?)

Just to clarify Pustular Postulant, the OP must have called the functions in a void or scalar context. sort is optimized to do nothing in that situation, so the functions naive and orcish return instantly. Read more for confirmation of Pustular Postulant's results and confirmation that running the sorts in scalar context give results similar to those of the OP.

my @unsorted = sort map { "foo$_.tla" } 0..999; Benchmark::cmpthese(5000, { naive => sub { my @a = naive @unsorted; }, orcish => sub { my @a = orcish @unsorted; }, schwartzian => sub { my @a = schwartzian @unsorted; }, guttros => sub { my @a = guttros @unsorted; }, });

gives me

Rate naive orcish schwartzian guttros naive 48.3/s -- -34% -46% -60% orcish 73.1/s 51% -- -19% -40% schwartzian 90.1/s 86% 23% -- -26% guttros 122/s 152% 67% 35% --

while

my @unsorted = sort map { "foo$_.tla" } 0..999; Benchmark::cmpthese(5000, { naive => sub { scalar naive @unsorted; }, orcish => sub { scalar orcish @unsorted; }, schwartzian => sub { scalar schwartzian @unsorted; }, guttros => sub { scalar guttros @unsorted; }, });

gives me

(warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) Rate schwartzian guttros orcish naive schwartzian 107/s -- -26% -100% -100% guttros 144/s 35% -- -100% -100% orcish 106383/s 99666% 73834% -- -0% naive 106383/s 99666% 73834% 0% --

That's why it's important to show one's work :)


Comment on Re^2: Choosing the right sort (Or: WUDsamatter?)
Select or Download Code
Re^3: Choosing the right sort (Or: WUDsamatter?)
by tlm (Prior) on Apr 14, 2005 at 05:28 UTC

    Yes, that's what gave me the idea of asking for the first element of sorted array, though in hindsight there are definitely clearer, more direct ways to force a list context than the one I chose.

    the lowliest monk

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://447649]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-12-22 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (133 votes), past polls