Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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 cooling their heels in the Monastery: (6)
As of 2024-04-16 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found