Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Algorithm Efficiency vs. Specialized Hardware?

by lhoward (Vicar)
on Jun 21, 2000 at 00:02 UTC ( #19091=note: print w/replies, xml ) Need Help??

in reply to Algorithm Efficiency vs. Specialized Hardware?

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
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; }

Replies are listed 'Best First'.
RE: Re: Algorithm Efficiency vs. Specialized Hardware?
by btrott (Parson) on Jun 21, 2000 at 00:15 UTC
    Wondering why the original benchmark didn't work as it should, lhoward said:
    > (I think its some sort of quoting/scoping problem, > but I'm not sure exactly what).
    It's a scoping problem. The quoted code (Grep2) isn't accessing the same %url hash as the other two pieces of code, which you could tell if you had warnings on. You get a warning about a "Useless use of list slice in void context" for that code.

    Why aren't the others in void context? Grep is accessing the lexical %url because it's lexical to the file scope, and the Grep subroutine is defined in this file. And Grep3 (the third one) is accessing the same lexical %url because the anonymous sub acts as a closure (I think).

    So Grep2 is looking at a different (not defined) %url, and is thus not actually a very good test in terms of benchmarking. :)

by Aighearach on Jun 21, 2000 at 00:56 UTC

    It should be noted that most of my tests used the new our package global in place of my. Well, it's a lexical alias to a package global, anyhow. Or, something close to that... ;)

    Also, all my code used -w and use strict;. I wouldn't code without them... because I've made that mistake and spent all day looking for a typo bug.

    Paris Sinclair    |    4a75737420416e6f74686572    |    205065726c204861636b6572
    I wear my Geek Code on my finger.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://19091]
and the monks are mute...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2017-01-23 01:04 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (190 votes). Check out past polls.