You asked: "BTW, does perl caches the string hash value?". No, I don't think so. And there is no need to do so because the C equation that calculates the integer hash value runs like a rocket! I've got C code that uses and benchmarks 2 of the recent Perl hash functions, but the computer that it is on, is dead right now! Bummer. To calculate the hash function is very, very fast. I experimented with different C formulations of the equation and found that gcc even on lowest optimize setting coded them essentially the same. On a modern Intel processor even a int*2 is as fast as a left shift 1. I was amazed, but decades of development have gone into that thing and literally millions of transistors!

Before writing extra code that you think will make a performance difference, benchmark the code and see where you are at. Start with the most straight forward HLL code that implements a reasonable algorithm. Use the features of the language because they have been highly optimized and are likely to produce good results with a clear algorithm.

An array access by index will be faster than a hash table lookup, but not by all that much unless you do this a bazillion times. BenchMark your code and see for yourself.

Update: in my experience the 80/20 or even the 90/10 rule applies. 10% of the code does 90% of the "real work". Forget about optimizing the 90%, you must find the 10% where it really matters.


In reply to Re^4: Generating Unique numbers from Unique strings by Marshall
in thread Generating Unique numbers from Unique strings by rjohn1

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.