Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: sort direction

by BrowserUk (Patriarch)
on Oct 11, 2005 at 15:14 UTC ( [id://499258]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re^3: sort direction
in thread sort direction

Well, using sort{$b-$a} @numbers rather than sort{$b<=>$a} @numbers seems to run quite a bit more quickly and produce the desired result:

perl> cmpthese -1, { '<=>' => q[@s1=sort{$b<=>$a} 1..10000], '-' => q[@s2=sort{$b - $a} 1..10000] };; Rate <=> - <=> 33.8/s -- -82% - 183/s 441% --

There are probably good reasons why this shoudln't be done outside of golf though.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Replies are listed 'Best First'.
Re^5: sort direction
by Perl Mouse (Chaplain) on Oct 12, 2005 at 04:49 UTC
    That result highly surprised me, because sort is optimized for the {$a <=> $b}, {$b <=> $a}, {$a cmp $b} and {$b cmp $a} blocks (in the sense it recognises those blocks and does the compare internally instead of calling the block).

    But then I realized that the current sort implementation is also implemented to take advantage of long runs of increasing/decreasing values - and in particulary sorted arrays.

    So I decided to run the benchmark again, this time with shuffled values:

    #!/usr/bin/perl use strict; use warnings; use Benchmark 'cmpthese'; use List::Util 'shuffle'; our @data = shuffle 1 .. 10000; cmpthese -1, { '<=>' => q[@s1=sort{$b<=>$a} @data], ' - ' => q[@s2=sort{$b - $a} @data], }; __END__ Rate - <=> - 36.5/s -- -30% <=> 52.3/s 43% --
    That's what I'd expect.
    Perl --((8:>*

      Sigh. Fooled again :)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      Then again, if you eliminate the optimisation advantage;

      #!/usr/bin/perl use strict; use warnings; use Benchmark 'cmpthese'; use List::Util 'shuffle'; our @data = shuffle 1 .. 10000; cmpthese -1, { '<=>' => q[@s1=sort sub{ $b<=>$a }, @data], ' - ' => q[@s2=sort sub{ $b - $a }, @data], }; __END__ P:\test>junk Rate <=> - <=> 36.9/s -- -2% - 37.4/s 2% -- P:\test>junk Rate - <=> - 36.3/s -- -3% <=> 37.4/s 3% -- P:\test>junk Rate - <=> - 35.8/s -- -0% <=> 35.8/s 0% -- P:\test>junk Rate <=> - <=> 36.8/s -- -0% - 36.9/s 0% --

      ... it makes no difference whether the block returns (-1,0,1) or (-n,0,n), which is more relavent to the original discussion.

      However, in C or asm, it might avoid an unnecessary booleanisation of a subtraction?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://499258]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.