http://qs321.pair.com?node_id=811563


in reply to Hash reference searching

This is a lot worse than it could be: exists {%{$HASH_REF}}->{$random_number}.

What that does is dereference the hash %{$HASH_REF} in the context of creating another hash reference { ... }, which is then checked. On every iteration, you copy the whole referenced hash into a new hash reference and then destroy it.

Write it this way instead: exists $HASH_REF->{$random_number}

Update: If I were testing this, I'd probably use Benchmark like so:

use Benchmark qw( cmpthese ); use List::Util qw( shuffle ); my $range = 100000; my $ITERS = 30000; my (%HASH, $HASH_REF); for(my $iter = 1; $iter <= $ITERS; $iter++){ my $random_number = int(rand($range)); $HASH{$random_number} = 1; $HASH_REF->{$random_number} = 1; } my @rand = map { int rand $range } 1 .. $ITERS; my @hits = shuffle keys %HASH; my @miss = grep { ! exists $HASH{$_} } 0 .. $range; cmpthese( -2, { hash_r => sub { map { exists $HASH{$_} } @rand }, ref_r => sub { map { exists $HASH_REF->{$_} } @rand }, hash_h => sub { map { exists $HASH{$_} } @hits }, ref_h => sub { map { exists $HASH_REF->{$_} } @hits }, hash_m => sub { map { exists $HASH{$_} } @miss }, ref_m => sub { map { exists $HASH_REF->{$_} } @miss }, } ); __END__ Rate ref_m hash_m ref_r ref_h hash_r hash_h ref_m 57.8/s -- -10% -56% -58% -59% -61% hash_m 64.2/s 11% -- -51% -54% -54% -56% ref_r 131/s 127% 105% -- -5% -6% -11% ref_h 139/s 140% 117% 6% -- -0% -6% hash_r 140/s 141% 117% 6% 0% -- -5% hash_h 147/s 155% 130% 12% 6% 6% --

This way I test the difference between hits and misses as well as the random chance test you have. From this, it appears that the difference between exists finding or not finding something is a lot bigger than the difference between operating on a hash or a reference to a hash. In fact, I'd say the differences between hashes and references is so small as to be within the margin of error (which I usually think of as 5–10% with Benchmark).

Update 2: Oops! Bad test. I was feeding in far more misses than hits. I should have said:

my @rand = ( map { int rand $range } 1 .. $ITERS )[0 .. $ITERS/2]; my @hits = ( shuffle keys %HASH )[0 .. $ITERS/2]; my @miss = ( grep { ! exists $HASH{$_} } 0 .. $range )[0 .. $ITERS/2];

Then the results are:

Rate ref_h hash_h ref_r ref_m hash_r hash_m ref_h 229/s -- -10% -13% -18% -22% -30% hash_h 254/s 11% -- -3% -9% -14% -22% ref_r 263/s 15% 3% -- -6% -11% -19% ref_m 279/s 22% 10% 6% -- -5% -14% hash_r 295/s 29% 16% 12% 6% -- -10% hash_m 326/s 42% 28% 24% 17% 11% --

Just the opposite of before. Misses are faster than hits. Hashes are faster than references, but not by much.

I might also note: performance testing is hard.