These kinds of questions are always best answered using a benchmark ;)
#!/usr/bin/perl -w
use strict;
use Benchmark qw(cmpthese);
sub returnhashref {
my %a=();
for (1 .. 1000) {
$a{$_}=$_;
}
return \%a;
}
cmpthese(-3,{
'Ref' => sub { my $a=returnhashref(); return $$a{245}},
'newhash' => sub { my $a=returnhashref(); my %a = %$a; return
+$a{245}}});
Benchmark: running Ref, newhash, each for at least 3 CPU seconds...
Ref: 3 wallclock secs ( 3.24 usr + 0.01 sys = 3.25 CPU) @ 38
+1.54/s (n=1240)
newhash: 3 wallclock secs ( 3.21 usr + 0.00 sys = 3.21 CPU) @ 21
+1.84/s (n=680)
Rate newhash Ref
newhash 212/s -- -44%
Ref 382/s 80% --
So using the ref is about 50% faster in this case. Btw. the easiest way of dereferencing is using the shortcut $$a{somekey} instead of $a->{somekey}
T
I
M
T
O
W
T
D
I
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.