in reply to Hashes and hash references

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)

Replies are listed 'Best First'.
Re^2: Hashes and hash references
by GrandFather (Saint) on Sep 04, 2005 at 19:09 UTC

    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.