Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by tobyink (Abbot)
on Oct 24, 2012 at 16:31 UTC ( #1000666=note: print w/ replies, xml ) Need Help??


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

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.

use v5.14; use strict; use warnings; use List::MoreUtils qw( part ); use Sort::Key qw( keysort ); use Benchmark ':all'; package Person { use Moo; has name => (is => 'ro'); has title => (is => 'ro'); } my @employees = map { Person::->new( name => int(rand(999999)), title => int(rand(2))?'Staff':'Manager', ) } 0..200; sub obvious { my @sorted = sort { if ($a->title =~ /Manager/ and not $b->title =~ /Manager/) { -1; } elsif ($b->title =~ /Manager/ and not $a->title =~ /Manager/) +{ 1; } else { $a->name cmp $b->name; } } @employees; } sub subtle { my @sorted = sort { ($a->title !~ /Manager/) - ($b->title !~ /Manager/) or $a->name cmp $b->name; } @employees; } sub functional { my @sorted = map { sort { $a->name cmp $b->name } @$_ } part { $_->title !~ /Manager/ } @employees; } sub sortkey { my @sorted = map { keysort { $_->name } @$_ } part { $_->title !~ /Manager/ } @employees; } sub pure_schwarz { my @sorted = map $_->[1], sort { $a->[0] cmp $b->[0] } map [ $_->title =~ /Manager/ ? "A".$_->name : "B".$_->name, $ +_ ], @employees; } cmpthese(1000, { obvious => \&obvious, subtle => \&subtle, functional => \&functional, sortkey => \&sortkey, pure_schwarz => \&pure_schwarz, });

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'


Comment on Re^4: Unusual sorting requirements; comparing three implementations.
Download Code
Re^5: Unusual sorting requirements; comparing three implementations.
by BrowserUk (Pope) on Oct 24, 2012 at 18:40 UTC
    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

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

      I'm on a single core Intel Atom N270 clocked at 1.60 GHz.

      "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"

      Slow?! Ahem...

                      Rate      moose plain_perl        moo
      moose       304878/s         --       -18%       -73%
      plain_perl  370370/s        21%         --       -67%
      moo        1136364/s       273%       207%         --
      

      Moo's accessors are stupidly fast because they use XS.

      Even disabling XS via BEGIN { $ENV{MOO_XS_DISABLE} = 1 }, Moo's accessors are almost on par with the plain Perl version (about 7% slower).

      As far as object construction goes, Moo is slower, but that's because Moo insists on some basic sanity checking as part of the constructor, which the plain Perl version shown above does not do.

      But anyway, that could go some way to explaining:

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

      On my side I'm using Moo, so accessors are cheap calls. The functional implementation makes more calls to Person::name than your implementation, so slower accessors will cause it to drag.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        Slow?! Ahem...

        Yes. Slow! (Be careful what you benchmark.):

        C:\test>type junk99.pl use Benchmark qw(cmpthese); { package Foo1; sub new { bless $_[1], $_[0] } sub foo { $_[0]{foo} } } { package Foo2; use Moo; has foo => (is => 'ro'); } our $foo1 = Foo1::->new({foo => 0}); our $foo2 = Foo2::->new({foo => 0}); cmpthese( -3, { plain_perl => q[ $foo1->foo for 1 .. 1000; ], moo => q[ $foo2->foo for 1 .. 1000; ], }); C:\test>junk99 Rate moo plain_perl moo 2065/s -- -13% plain_perl 2381/s 15% --

        (Think about it: Why would calling into C; unboxing perl vars to c vars ; and then calling the perl hash apis to access the hash fields (as C vars); and then wrapping those back into perl wrappers before returning; be quicker than a dedicated opcode -- written in C -- to access the hash?)


        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

        Of course, we should really throw in a truly "plain perl" example as well:

        C:\test>type junk99.pl use Benchmark qw(cmpthese); { package Foo1; sub new { bless $_[1], $_[0] } sub foo { $_[0]{foo} } } { package Foo2; use Moo; has foo => (is => 'ro'); } { package Foo3; use Moose; has foo => (is => 'ro'); } our $foo1 = Foo1::->new({foo => 0}); our $foo2 = Foo2::->new({foo => 0}); our $foo3 = Foo3::->new({foo => 0}); our $foo4 = { foo => 0 }; cmpthese( -3, { OO_perl => q[ $foo1->foo for 1 .. 1000; ], moo => q[ $foo2->foo for 1 .. 1000; ], moose => q[ $foo3->foo for 1 .. 1000; ], plain_perl => q[ $foo4->{foo} for 1 .. 1000; ], }); C:\test>junk99 Rate moose moo OO_perl plain_perl moose 2004/s -- -3% -15% -74% moo 2068/s 3% -- -13% -73% OO_perl 2366/s 18% 14% -- -69% plain_perl 7624/s 280% 269% 222% --

        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

Re^5: Unusual sorting requirements; comparing three implementations.
by BrowserUk (Pope) on Oct 24, 2012 at 18:50 UTC

    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://1000666]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2014-09-17 01:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (56 votes), past polls