http://www.perlmonks.org?node_id=1000597


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

Replies are listed 'Best First'.
Re^2: Unusual sorting requirements; comparing three implementations.
by afoken (Chancellor) 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'