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

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


Comment on Re^3: Unusual sorting requirements; comparing three implementations.
Download Code
Re^4: Unusual sorting requirements; comparing three implementations.
by tobyink (Abbot) on Oct 24, 2012 at 16:31 UTC

    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

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

      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