Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Unusual sorting requirements; comparing three implementations.

by afoken (Parson)
on Oct 24, 2012 at 11:19 UTC ( #1000608=note: print w/ replies, xml ) Need Help??


in reply to Re: Unusual sorting requirements; comparing three implementations.
in thread Unusual sorting requirements; comparing three implementations.

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". ;-)


Comment on Re^2: Unusual sorting requirements; comparing three implementations.
Select or Download Code
Re^3: Unusual sorting requirements; comparing three implementations.
by BrowserUk (Pope) on Oct 24, 2012 at 11:31 UTC

    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'
        Not sure if your computer's architecture is radically different to mine or something, ...

        The first difference I notice is that your fastest iterating solution -- 671/s -- is only half as fast as my fastest iterating solution -- 1100/s.

        I seriously doubt my 3 y/o Core2Quad Q6600 @2.4GHz is twice as fast as your hardware.

        So the major difference probably comes down to the fact that I couldn't be bothered to install yet another slow-accessor constructor, so I manually code my Person Class:

        package Person { sub new{ my $p = shift; bless { @_ }, $p } sub name{ $_[0]->{name } } sub title{ $_[0]->{title} } }

        After that, why your results should be relatively different to mine I have no idea:

        But mine are consistent:

        C:\test>date /t & time/t && junk87 24/10/2012 19:39 Rate obvious subtle functional x obvious 189/s -- -16% -60% -83% subtle 226/s 19% -- -52% -80% functional 471/s 149% 109% -- -57% x 1101/s 482% 388% 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

        Just for fun, I installed salva's Sort::Key and added:

        sub x2 { my @sorted = keysort { $_->title =~ /Manager/ ? 'A'.$_->name : 'B'.$_->name } @employees; }

        With these results:

        C:\test>junk87 Rate obvious subtle functional x +x2 obvious 186/s -- -20% -61% -83% -8 +9% subtle 232/s 24% -- -51% -79% -8 +6% functional 477/s 156% 106% -- -56% -7 +2% x 1085/s 483% 368% 127% -- -3 +7% x2 1717/s 822% 641% 260% 58% +--

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000608]
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: (3)
As of 2014-12-23 05:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (135 votes), past polls