Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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:

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.


In reply to Re: Hash reference searching by kyle
in thread Hash reference searching by seggy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found