Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

by gods
on Feb 11, 2000 at 00:06 UTC ( #3333=superdoc: print w/replies, xml ) Need Help??

Your benchmark is utterly bogus.

First, you use the q{} form of benchmark and yet you try to access a lexical variable that will not be in scope when the code is evaled. So your code benchmarks sorting an empty list! (The counts in the millions per second range should have tipped you off that something was wrong.)

Next the 'st' implementation has a bug in it so it doesnt actually sort correctly $a->[0] <=> $a->[0] should read $a->[0] <=> $b->[0], and the 'bare' implementation doesnt do the full sort either as it doesnt sort based on the word as well as the 'E' count.

Once the benchmark is fixed up to actually test similar things against similar things, and to use a better non empty data set we see that the GRT crushes the competition, and that the ST greatly outperforms the uncached code.

Benchmark: running bare, grt, st, each for at least 3 CPU seconds... bare: 3 wclk secs ( 3.02 usr + 0.00 sys = 3.02 CPU) @ 4.63/s + (n=14) grt: 4 wclk secs ( 3.15 usr + 0.00 sys = 3.15 CPU) @ 15.54/s + (n=49) st: 3 wclk secs ( 3.13 usr + 0.00 sys = 3.13 CPU) @ 10.21/s + (n=32) Rate bare st grt bare 4.63/s -- -55% -70% st 10.2/s 120% -- -34% grt 15.5/s 235% 52% --

And using the exact same data set as you did we see that the GRT still wins, even if 'bare' outperforms 'st'.

Rate st bare grt st 18009/s -- -21% -49% bare 22661/s 26% -- -36% grt 35616/s 98% 57% --
#!/usr/bin/perl use strict; use warnings; use Text::Wrap qw(wrap); use Benchmark qw(cmpthese); my @words = qw( EMBOSOM EMBOWED EMBOWEL EMBOWER EMBRACE EMBROIL EMBROWN EMBRUED EMBRUE +S EMBRUTE EMBRYON EMBRYOS EMENDED EMENDER EMERALD EMERGED EMERGES EMERIE +S EMERITA EMERITI EMERODS EMEROID EMERSED EMETICS EMETINE EMETINS EMEUTE +S EMIGRES EMINENT EMIRATE EMITTED EMITTER EMODINS EMOTERS EMOTING EMOTIO +N EMOTIVE EMPALED EMPALER EMPALES EMPANEL EXFOLIATIONS EXHAUSTIVELY EXHAUSTIVITY EXHIBITIONER EXHILARATING EXHILARATION EXHILARATIVE EXHORTATIONS EXIGUOUSNESS EXOBIOLOGIES EXOBIOLOGIST EXONERATIONS EXONUCLEASES EXOPEPTIDASE EXOPHTHALMIC ); my $test={ st => sub { my @sorted = map { $_->[1] } sort { $a->[0] <=> $b->[0] || $a->[1] cmp $b->[1] } map { [ tr/eE/eE/, $_ ] } @words; }, grt => sub { my @sorted = map { substr($_, 4) } sort map { pack("LA*", tr/eE/eE/, $_) } @words; }, bare => sub { my @sorted = sort { ($a =~ tr/eE/eE/) <=> ($b =~ tr/eE/eE/) || $a cmp $b } @words; } }; foreach my $t (keys %$test) { print "Test '$t'\n",wrap("\t","\t",join(", ",$test->{$t}->())),"\n +"; } @words=map { ( $_ x 100 ) x 100 } @words; cmpthese(-3,$test);
This is perl, v5.6.1 built for MSWin32-x86-multi-thread

The moral of the story is a common one: Always test your benchmarks. Always double check that what you are comparing is equivelent. Always be careful with selecting the data set you benchmark against. If you benchmark a code snippet and not a subref then you should excercise even more caution. (Generally I dont think its a good idea actually.)


<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

In reply to Re: Re: Advanced Sorting - GRT - Guttman Rosler Transform by demerphq
in thread Advanced Sorting - GRT - Guttman Rosler Transform by demerphq

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (5)
    As of 2016-09-01 05:21 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found