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

Re: Unusual sorting requirements; comparing three implementations.

by BrowserUk (Pope)
on Oct 24, 2012 at 10:36 UTC ( #1000597=note: print w/ replies, xml ) Need Help??


in reply to Unusual sorting requirements; comparing three implementations.

Ignore. Dumb mistake. Corrected:

... sub x { my @sorted = map $_->[1], sort{ $a->[0] cmp $b->[0] } map[ $_->title eq 'Manager' ? 'A'.$_->name : 'B'.$_->name, $_ ], @employees; } cmpthese( -1, { obvious => \&obvious, subtle => \&subtle, functional => \&functional, x => \&x, }); __END__ C:\test>junk87 Rate obvious subtle functional x obvious 185/s -- -15% -60% -83% subtle 218/s 18% -- -54% -80% functional 469/s 153% 115% -- -58% x 1111/s 500% 411% 137% --

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong


Comment on Re: Unusual sorting requirements; comparing three implementations.
Download Code
Re^2: Unusual sorting requirements; comparing three implementations.
by afoken (Parson) on Oct 24, 2012 at 11:19 UTC
    sub x { my @sorted = map $_->[1], sort{ $a->[0] cmp $b->[0] } map[ $_->title eq 'Manager' ? 'A'.$_->name : 'B'.$_->name, $_ ], @employees; }

    Faster, but not correct for the original problem. tobyink's Test::More script tested for ->title =~ /Manager/, not for ->title eq 'Manager', and so all those non-generic Manager employees, like the Sales Manager, the Finance Manager, and the Marketing Manager will be sorted as NON-Manager employees. tobyink's benchmark script wrongly reduces the possible titles to "Staff" and "Manager".

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Correcting that makes barely any difference to the performance:

      sub x { my @sorted = map $_->[1], sort{ $a->[0] cmp $b->[0] } map[ $_->title =~ /Manager/ ? 'A'.$_->name : 'B'.$_->name, $_ ], @employees; } cmpthese( -1, { obvious => \&obvious, subtle => \&subtle, functional => \&functional, x => \&x, }); C:\test>junk87 Rate obvious subtle functional x obvious 183/s -- -18% -60% -83% subtle 224/s 22% -- -51% -79% functional 458/s 150% 105% -- -57% x 1070/s 483% 378% 134% --

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

        Not sure if your computer's architecture is radically different to mine or something, but for me, this technique is still slightly slower than the functional approach, and much slower than keysort.

        Results:

                      Rate     obvious       subtle pure_schwarz  functional     sortkey
        obvious      133/s          --         -28%         -60%        -65%        -80%
        subtle       186/s         39%           --         -45%        -52%        -72%
        pure_schwarz 337/s        153%          81%           --        -12%        -50%
        functional   383/s        187%         107%          14%          --        -43%
        sortkey      671/s        403%         262%          99%         75%          --
        
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-04-20 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls