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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |