Octavian Rasnita wrote:
Hi,

Hello,

Which would be the best method for creating a better program, accessing the
content of a hash or a dereference like $hash{key}, $ref->{key} in many
places in the script, or using:

my $value = $hash{key}, ... my $value = $ref->{key} then using $value for
many times in the rest of the script?

Would be the second way faster, or it won't make any difference?

Here are several benchmark results (since the results can be tainted by a busy system ;p):


Benchmark: timing 2000000 iterations of deref only, deref to var, key only, key to var...
deref only: 0.935947 wallclock secs ( 1.03 usr + 0.00 sys = 1.03 CPU) @ 1941747.57/s (n=2000000)
deref to var: 1.04673 wallclock secs ( 0.97 usr + -0.01 sys = 0.96 CPU) @ 2083333.33/s (n=2000000)
key only: 0.979745 wallclock secs ( 0.98 usr + -0.01 sys = 0.97 CPU) @ 2061855.67/s (n=2000000)
key to var: 1.0059 wallclock secs ( 1.00 usr + 0.00 sys = 1.00 CPU) @ 2000000.00/s (n=2000000)
Rate deref only key to var key only deref to var
deref only 1941748/s -- -3% -6% -7%
key to var 2000000/s 3% -- -3% -4%
key only 2061856/s 6% 3% -- -1%
deref to var 2083333/s 7% 4% 1% --


Benchmark: timing 2000000 iterations of deref only, deref to var, key only, key to var...
deref only: 0.903167 wallclock secs ( 0.87 usr + 0.00 sys = 0.87 CPU) @ 2298850.57/s (n=2000000)
deref to var: 0.974892 wallclock secs ( 0.97 usr + 0.00 sys = 0.97 CPU) @ 2061855.67/s (n=2000000)
key only: 1.05517 wallclock secs ( 0.98 usr + 0.00 sys = 0.98 CPU) @ 2040816.33/s (n=2000000)
key to var: 1.00371 wallclock secs ( 0.98 usr + 0.00 sys = 0.98 CPU) @ 2040816.33/s (n=2000000)
Rate key to var key only deref to var deref only
key to var 2040816/s -- -0% -1% -11%
key only 2040816/s 0% -- -1% -11%
deref to var 2061856/s 1% 1% -- -10%
deref only 2298851/s 13% 13% 11% --



Benchmark: timing 2000000 iterations of deref only, deref to var, key only, key to var...
deref only: 0.874569 wallclock secs ( 0.87 usr + 0.00 sys = 0.87 CPU) @ 2298850.57/s (n=2000000)
deref to var: 0.994712 wallclock secs ( 1.01 usr + 0.00 sys = 1.01 CPU) @ 1980198.02/s (n=2000000)
key only: 1.05804 wallclock secs ( 1.01 usr + 0.00 sys = 1.01 CPU) @ 1980198.02/s (n=2000000)
key to var: 1.00529 wallclock secs ( 0.92 usr + 0.00 sys = 0.92 CPU) @ 2173913.04/s (n=2000000)
Rate deref to var key only key to var deref only
deref to var 1980198/s -- -0% -9% -14%
key only 1980198/s 0% -- -9% -14%
key to var 2173913/s 10% 10% -- -5%
deref only 2298851/s 16% 16% 6% --



Benchmark: timing 2000000 iterations of deref only, deref to var, key only, key to var...
deref only: 0.98188 wallclock secs ( 0.93 usr + 0.00 sys = 0.93 CPU) @ 2150537.63/s (n=2000000)
deref to var: 1.07344 wallclock secs ( 1.05 usr + 0.00 sys = 1.05 CPU) @ 1904761.90/s (n=2000000)
key only: 1.16528 wallclock secs ( 1.11 usr + 0.00 sys = 1.11 CPU) @ 1801801.80/s (n=2000000)
key to var: 1.10399 wallclock secs ( 1.09 usr + 0.00 sys = 1.09 CPU) @ 1834862.39/s (n=2000000)
Rate key only key to var deref to var deref only
key only 1801802/s -- -2% -5% -16%
key to var 1834862/s 2% -- -4% -15%
deref to var 1904762/s 6% 4% -- -11%
deref only 2150538/s 19% 17% 13% --



Benchmark: timing 2000000 iterations of deref only, deref to var, key only, key to var...
deref only: 0.85706 wallclock secs ( 0.90 usr + -0.01 sys = 0.89 CPU) @ 2247191.01/s (n=2000000)
deref to var: 0.982999 wallclock secs ( 0.94 usr + 0.00 sys = 0.94 CPU) @ 2127659.57/s (n=2000000)
key only: 0.983509 wallclock secs ( 0.95 usr + 0.00 sys = 0.95 CPU) @ 2105263.16/s (n=2000000)
key to var: 1.00118 wallclock secs ( 0.96 usr + 0.00 sys = 0.96 CPU) @ 2083333.33/s (n=2000000)
Rate key to var key only deref to var deref only
key to var 2083333/s -- -1% -2% -7%
key only 2105263/s 1% -- -1% -6%
deref to var 2127660/s 2% 1% -- -5%
deref only 2247191/s 8% 7% 6% --



Where

key to var:

my %hash = (key=>123);
my $var = $hash{key};
for(1..100) {
   print $var;
}

deref to var:

my %hash = (key=>123);
my $ref = \%hash;
my $var = $ref->{key};
for(1..100) {
   print $var;
}

key only:

my %hash = (key=>123);
for(1..100) {
   print $hash{key};
}


deref only:

my %hash = (key=>123);
my $ref = \%hash;
for(1..100) {
   print $ref->{key};
}


I'd personally just use $hash{key} if I had %hash and $ref->{key} if I had a hash reference and not assign it to a variable at all, unless the key may change then I'd use a variable and assign it using the key if I had %hash and the deref if I had a hash ref, but thats just me :)


HTH :)

Lee.M - JupiterHost.Net

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to