Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Hashes and hash references

by Anonymous Monk
on Sep 04, 2005 at 16:49 UTC ( #489059=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I was reading in Effective Perl Programming about creating hash references such as this:
$student = {}; $student->{last} = 'Smith';
I believe he is saying that this is more efficient to be passed to a subroutine, and I agree with that.
What puzzles me though is that you can, and should, always pass a reference to a hash, or array, to a subroutine anyway:
&sub(\%hash, \@array);
I have also heard that it is faster to create and use a hash reference in general than using a "regular" hash such as:
my %student = {}; $student{'last'} = 'Smith';
This might be a silly question but is it more efficient to declare and uses hashes that way (reference) and if it is more efficient then why do we have the possibility to declare and use them the "regular" way?
Is it one of those TIMTOWTDI things?

Confused Monk

Comment on Hashes and hash references
Select or Download Code
Re: Hashes and hash references
by dave_the_m (Parson) on Sep 04, 2005 at 17:17 UTC
    This might be a silly question but is it more efficient to declare and uses hashes that way (reference)
    No, a direct hash will be marginally faster.

    PS this is wrong:

    my %student = {};
    it should be
    my %student = ();
    or even just
    my %student;
    as the hash is created empty already

    Dave.

      oops!...

      You are right. See I got confused with the reference construct ;-)

      Confused Monk

Re: Hashes and hash references
by pg (Canon) on Sep 04, 2005 at 17:39 UTC

    Depends on what we are talking:

    Assignment: (reference appears to be consistantly slower than regular)

    use Benchmark qw (timethese); timethese(10, {"regular" => \&regular, "reference" => \&reference}); sub regular { my %hash; for my $i (1 .. 100000) { $hash{$i} = $i; } } sub reference { my $hash; for my $i (1 .. 100000) { $hash->{$i} = $i; } }
    Benchmark: timing 10 iterations of reference, regular... reference: 8 wallclock secs ( 7.76 usr + 0.16 sys = 7.92 CPU) @ 1 +.26/s (n=1 0) regular: 8 wallclock secs ( 7.20 usr + 0.14 sys = 7.34 CPU) @ 1 +.36/s (n=1 0) Benchmark: timing 10 iterations of reference, regular... reference: 9 wallclock secs ( 7.67 usr + 0.17 sys = 7.84 CPU) @ 1 +.27/s (n=1 0) regular: 8 wallclock secs ( 7.23 usr + 0.08 sys = 7.31 CPU) @ 1 +.37/s (n=1 0)

    Assignment + Passing as ref: (reference appears to be faster than regular)

    use Benchmark qw (timethese); timethese(10, {"regular" => \&regular, "reference" => \&reference}); sub regular { my %hash; for my $i (1 .. 100000) { $hash{$i} = $i; } helper(\%hash); } sub reference { my $hash; for my $i (1 .. 100000) { $hash->{$i} = $i; } helper($hash); } sub helper { my $hash = shift; }
    Benchmark: timing 10 iterations of reference, regular... reference: 8 wallclock secs ( 7.72 usr + 0.14 sys = 7.86 CPU) @ 1 +.27/s (n=1 0) regular: 9 wallclock secs ( 7.91 usr + 0.08 sys = 7.98 CPU) @ 1 +.25/s (n=1 0) Benchmark: timing 10 iterations of reference, regular... reference: 8 wallclock secs ( 7.70 usr + 0.11 sys = 7.81 CPU) @ 1 +.28/s (n=1 0) regular: 9 wallclock secs ( 7.95 usr + 0.06 sys = 8.02 CPU) @ 1 +.25/s (n=1 0)

      I like cmpthese results rather than timethese so I've reworked your benchmark a little.

      The parameter passing tests were doing a lot of non-parameter passing work so I made the variables global (using our variables) and used the constructed hashes from the first two tests as the test variables for the passing tests.

      Rate reference regular reference 8.00/s -- -8% regular 8.73/s 9% -- Rate regularPass referencePass regularPass 7.19/s -- -33% referencePass 10.7/s 49% --

      Perl is Huffman encoded by design.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://489059]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2014-09-16 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (155 votes), past polls