Hello perlmonks, this is my first post so I hope I am posting on the correct forum section. So it seems that when executing an "exists" call on a hash reference (to check whether the corresponding value exists) the amount of time needed to complete the call is significantly higher compared to the same operation performed on a normal hash (not a hash reference). Is this behavior expected? Please check the following simple code highlighting what I described.
#!/usr/bin/perl -w use strict; use warnings; use Time::Local; 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 $TIME = time; for(my $iter = 1; $iter <= $ITERS; $iter++){ my $random_number = int(rand($range)); if( exists $HASH{$random_number} ){;} } my $ctime = time; my $exec_time = $ctime - $TIME; print 'Hash search :'."$exec_time\n"; $TIME = time; for(my $iter = 1; $iter <= $ITERS; $iter++){ my $random_number = int(rand($range)); if( exists {%{$HASH_REF}}->{$random_number} ){;} } $ctime = time; $exec_time = $ctime - $TIME; print 'Hashref search :'."$exec_time\n";

In reply to 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.