The good thing about using stringified representations of hashes for caching purposes, is that it makes each lookup O(1) (with a largish constant).

Whereas, if you used one of the apparently more efficient, short-circuiting concurrent traversal methods, you have to perform that traversal against every other item (hash) in your cache. And that's O(N) (with a smaller constant). The stringified method wins hands down

One tip if you decide to write your own stringifier, (I'd use Storable::freeze() which is generally much faster than any of the to-text serialisers), prefix your stringified hash with the number of keys it contains. There's no point in comparing the whole stringified hashes if they have different numbers of keys.

Update: Actually, you might as well throw the length of the stringified hash up front also. Something like:

use Storable qw[ freeze ]; my %cache; sub memoised { my $href = shift; my $cacheKey = pack 'NN/a*', scalar keys %$href, freeze $href; return $cache{ $cacheKey } if exists $cache{ $cacheKey }; ## calculate value for href return $cache{ $cacheKey } = $value; }

That should short-circuit any hash collision string compares very quickly in all but the most nearly identical cases. Mind you, you'd be devilishly unlucky to have two nearly identical stringified hashes hash to the same bucket.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Fastest data structure compare? by BrowserUk
in thread Fastest data structure compare? by swartz

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.