Welcome to the Monastery | |
PerlMonks |
Re: Hash reference searchingby kyle (Abbot) |
on Dec 07, 2009 at 17:18 UTC ( [id://811563]=note: print w/replies, xml ) | Need Help?? |
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:
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:
Then the results are:
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.
In Section
Seekers of Perl Wisdom
|
|