Can one or more wise monks advise on this issue?

I have a very large hash (over 3 GB) that may be accessed 100,000 times or more during the life of a script (once for each record input to the script). Instead of accessing the large hash so many times, each time I run the script I could build a much smaller hash, with at most a few thousand keys, based on the initial response to the search of the large hash. I could then see if a key exists in the small hash before even referencing the large hash.

Since hash access is normally O(1), this may be a non-starter of an idea. But the large hash does have collisions, and I would like to find a way to speed up my script (no, haven't done any profiling). For what it's worth, the values in the large hash are strings of perhaps 80 characters.

Now if I created a small hash, it would contain only a dozen or so values, so I am concerned about hash collisions. One way to avoid collisions in my small hash would be to make each value an array (probably a reference to an anonymous array) and make the second element of the array a unique value. That seems rather kludgy to me, but I should think it would eliminate hash collisions at the price of significant overhead in creating the smaller hash.

So, to my questions. Is building a small hash based on results of accessing a large hash likely to help speed up my script? If so, what would be the best way to ensure that the smaller hash has unique values given that the values of interest are just short ASCII stings that are not at all unique?

A blessing on all monks with insight.


In reply to Small Hash a Gateway to Large Hash? by lsherwood

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.