Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Ref to hash entries are faster?

by ikegami (Patriarch)
on Nov 29, 2006 at 23:29 UTC ( [id://586841]=note: print w/replies, xml ) Need Help??


in reply to Re: Ref to hash entries are faster?
in thread Ref to hash entries are faster?

Not a fair comparison. You're creating an entirely new SV every time, whereas the OP was using an existing ref.
$Contracts{$key}[STATE]
vs
my $r = $Contracts{$key}; $r->[STATE]
is not similar to
$Contracts{$key}
vs
my $r = \$Contracts{$key}; $$r

Update:

My benchmark:

use strict; use warnings; use Benchmark qw( cmpthese ); use constant NUM_KEYS => $ARGV[0]; use constant NUM_ACCESSES => $ARGV[1]; use constant TEST_TIME => $ARGV[2]; our %contracts = map { $_ => [ 0 ] } 1 .. NUM_KEYS; my $lookup = ' foreach (keys %contracts) { _____ } '; $lookup =~ s/_____/'$a = $contracts{$_}[0];' x NUM_ACCESSES/e; my $refit = ' foreach (keys %contracts) { my $r = $contracts{$_}; _____ } '; $refit =~ s/_____/'$a = $r->[0];' x NUM_ACCESSES/e; cmpthese(TEST_TIME, { lookup => $lookup, refit => $refit, });
My results:
>perl 586841.pl 500 4 -5 Rate lookup refit lookup 1060/s -- -4% refit 1101/s 4% -- >perl 586841.pl 500 4 -5 Rate lookup refit lookup 1053/s -- -5% refit 1105/s 5% -- >perl 586841.pl 500 6 -5 Rate lookup refit lookup 772/s -- -11% refit 869/s 13% -- >perl 586841.pl 500 6 -5 Rate lookup refit lookup 775/s -- -11% refit 873/s 13% -- >perl 586841.pl 500 10 -5 Rate lookup refit lookup 496/s -- -19% refit 614/s 24% -- >perl 586841.pl 500 10 -5 Rate lookup refit lookup 499/s -- -19% refit 613/s 23% --

Verdict: It's a fruitful optimization, but it's not ground breaking.

Replies are listed 'Best First'.
Re^3: Ref to hash entries are faster?
by GrandFather (Saint) on Nov 30, 2006 at 00:06 UTC

    Your benchmark is including the loop overhead - which is appropriate for "real" code, but dilutes the difference between the two access methods. Altering the appropriate parts of your code to:

    my $lookup = ' _____ '; $lookup =~ s/_____/'$a = $contracts{1}[0];' x NUM_ACCESSES/e; my $refit = ' my $r = $contracts{1}; _____ '; $refit =~ s/_____/'$a = $r->[0];' x NUM_ACCESSES/e;

    and using your last test set I get:

    Rate lookup refit lookup 245666/s -- -32% refit 360597/s 47% --

    which is double the difference shown with the loop overhead included, and is comparable with the result from my benchmark.

    Ain't benchmarks fun, almost as good as statictics! :-D


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://586841]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-18 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found