The first problem is that your arrays are lexical, but you are using strings not sub for your tests. That means that they cannot not be captured as closures. So, you are effectively testing empty arrays and hashes which is why the iteration counts are so high. To test this, insert a print statement inside the first test and run the test for a count of 1:

... Benchmark::cmpthese( 1, { arrayLookup => q{ print scalar( @test_array ), ' : ', scalar( @indexes ); my $val = &arrayLookup( \@test_array, \@indexes ); }, ... __OUPUT__ c:\test>junk3 0 : 0 (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) Rate shuffled_arrayLookup shuffled_ +hashLookup hashLookup arrayLookup shuffled_arrayLookup 1000000000000000/s -- + 0% 0% 0% shuffled_hashLookup 1000000000000000/s 0% + -- 0% 0% hashLookup 1000000000000000/s 0% + 0% -- 0% arrayLookup 1000000000000000/s 0% + 0% 0% --

If you are going to use strings, then your external variables have to be declared with our not my. Doing that and re-running the above sanity check gives:

c:\test>junk3 107 : 201 (warning: too few iterations for a reliable count +) (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) Rate shuffled_arrayLookup shuffled_ +hashLookup hashLookup arrayLookup shuffled_arrayLookup 1000000000000000/s -- + 0% 0% 0% shuffled_hashLookup 1000000000000000/s 0% + -- 0% 0% hashLookup 1000000000000000/s 0% + 0% -- 0% arrayLookup 1000000000000000/s 0% + 0% 0% --

Running the benchmark once that change is made gives:

c:\test>junk3 Rate shuffled_hashLookup hashLookup arrayLookup + shuffled_arrayLookup shuffled_hashLookup 2113/s -- -0% -3% + -5% hashLookup 2113/s 0% -- -3% + -5% arrayLookup 2181/s 3% 3% -- + -2% shuffled_arrayLookup 2233/s 6% 6% 2% + --

Not a whole heap of difference in the percentages, but take a close look at the two orders of magnitude drop in the number of iterations/second.


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^2: How do I efficiently predeclare a hash with a set of keys by BrowserUk
in thread How do I efficiently predeclare a hash with a set of keys by lupey

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.