Caches are almost exclusively the territory of hashes. grep is fast, but it has to do searching which has a O(log n) at best. Hashing does searching at (essentially) O(1). That's about as fast as is possible (that we know of).

Now, the classic tradeoff for speed is memory. You have to remember more with hashes, cause every value has a key associated with it. Arrays, on the other hand, just store that value.

From what you've described, speed is much more important to you than memory, especially since you reset your cache every so often. (This doesn't free the memory to the OS, but it does free it back to the Perl interpreter.)

Now, there's an even better thing with hashes with regards to memory. Perl pre-allocates a certain amount of space whenever you use any variable, be it a scalar, array, or hash. (This, as you might guess, is for speed considerations.) With arrays, the number I've most often heard is that around 50 scalars-worth are pre-allocated. Hashes pre-allocate 16 scalars-worth (or less) - 2 scalars per bucket. Now, hashes grow pretty quickly (for example, 65 scalars would need space for 100 in a list, but 128 in a hash), but if you're not going beyond 32, you're still in the same ballpark for space.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.


In reply to Re: Internals question - "exists" for hash keys versus grep'ing array by dragonchild
in thread Internals question - "exists" for hash keys versus grep'ing array by Limbic~Region

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.