Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I believe the original benchamrk code as posted is broken. Thanks to Aighearach for questioning my earlier results and leading me along this path.

Consider this code:

my $now = 8; my %url = ( monday => { @{[map(($_,1), (1..1000))]} } ); timethese(0, { Grep => 'Grep();', Grep2 => q{(sort grep {$_ <= $now} keys %{$url{"monday"}})[-1]; +}, Grep3 => sub{(sort grep {$_ <= $now} keys %{$url{"monday"}})[-1 +];} }); sub Grep{ $now = (sort grep {$_ <= $now} keys %{$url{'monday'}})[-1]; }
The output from that:
Benchmark: running Grep, Grep2, Grep3, each for at least 3 CPU seconds...
      Grep:  4 wallclock secs ( 3.00 usr +  0.02 sys =  3.02 CPU) @ 219.21/s (n=662)
     Grep2:  4 wallclock secs ( 3.14 usr +  0.01 sys =  3.15 CPU) @ 356475.56/s (n=1122898)
     Grep3:  4 wallclock secs ( 3.30 usr +  0.04 sys =  3.34 CPU) @ 213.47/s (n=713)
shows that something is definitely up, the "inline sub" and "called sub" take the same time, but the "inline quoted" version is unbelievably faster. I think it has something to do with the inline quoted version not really working the way the original poster intended (I think its some sort of quoting/scoping problem, but I'm not sure exactly what).

I have rerun my benchmarks with all the routines as "inline subs" and the results follow:
dataset sizeGrepMaxTernary
1002440.20/s2511.96/s2495.22/s
1000207.32/s222.81/s216.31/s
1000015.31/s16.77/s16.61/s
And here is the code I used to run the test:

my %url; my $now = 8; my @size=(100,1000,10000); foreach(@size){ my $size=$_; print "size=$size\n"; %url = ( monday => { @{[map(($_,1), (1..$size))]} } ); timethese(0, { Grep => sub {(sort grep {$_ <= $now} keys %{$url{"mond +ay"}})[-1];}, Ternary => sub {($now < $_ && $_ < 8 ? $_ : $now) for key +s %{$url{"monday"}};}, Max => sub {foreach ( keys %{$url{"monday"}} ) { $now + = $_ if $_ > $now };} }); undef %url; }

In reply to Re: Algorithm Efficiency vs. Specialized Hardware? by lhoward
in thread Algorithm Efficiency vs. Specialized Hardware? by Russ

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-24 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found