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.
-
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.