A test expression like
    map { exists $HASH{$_} } @rand
seems to include a lot of superfluous temporary list construction and destruction redundant overhead extraneous activity.

My approach would be simpler (please forgive a bit of slapdashery):

>perl -wMstrict -le "use Benchmark qw(cmpthese); my %HASH; my $not_key = 'not_key'; $HASH{ rand() } = 1; compare(); $HASH{ rand() } = 1 for 0 .. 30_000; compare(); sub compare { print '------------------'; print 'hash keys/buckets: ' . %HASH; my ($is_key) = each %HASH; ! exists $HASH{$not_key} or die qq{$not_key should not exist}; exists $HASH{$is_key} or die qq{$is_key should exist}; print qq{test keys '$is_key' and '$not_key' seem ok}; my $HR = \%HASH; cmpthese(-2, { hash_hit => sub { exists $HASH{$is_key} }, hash_miss => sub { exists $HASH{$not_key} }, href_hit => sub { exists $HR->{$is_key} }, href_miss => sub { exists $HR->{$not_key} }, }); } " ------------------ hash keys/buckets: 1/8 test keys '0.408477783203125' and 'not_key' seem ok Rate hash_miss href_miss href_hit hash_hit hash_miss 3305141/s -- -6% -22% -42% href_miss 3520236/s 7% -- -16% -39% href_hit 4215045/s 28% 20% -- -27% hash_hit 5746274/s 74% 63% 36% -- ------------------ hash keys/buckets: 14778/32768 test keys '0.091217041015625' and 'not_key' seem ok Rate href_miss href_hit hash_miss hash_hit href_miss 3392515/s -- -18% -21% -36% href_hit 4126900/s 22% -- -3% -23% hash_miss 4272362/s 26% 4% -- -20% hash_hit 5329295/s 57% 29% 25% --

Hashes still seem faster than references, but hits seem faster than misses. Overall differences are still not compelling.

Update: The results above support the notion that hash key lookup (e.g., with exists) is O(1), so the idea of direct or indirect hash lookup being 'faster' might not have much meaning even if the timing differences were more significant.


In reply to Re^2: Hash reference searching by AnomalousMonk
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":



  • 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.