Your benchmark is still fatally flawed.

The major cost of using keys, is the requests for extra memory from the OS in order to build the list of keys. This is a one-off cost, that only occurs the first time the process requests that memory from the OS. By running your code in a loop (using Benchmark), you are amortising that costs over many re-runs, thereby reducing its significance. And by running both methods in the same benchmark, your are further amortising the penalty of that memory allocation over the each method runs which don't need it, thus further skewing the results.

This is a Windows-specific benchmark (due to the use of tasklist.exe in memUsed()) but should be readily adaptable to *nix:

On my system, for 1 million keys, for / keys required 56MB of extra memory and so is 10x slower than while / each:

c:\test>each-keys-b -M1=keys -M2=pairs -N=1e6 hash built, starting timer Took 11.663000 and 56.145MB extra memory for 1000000 using keys/pairs +method c:\test>each-keys-b -M1=each -M2=pairs -N=1e6 hash built, starting timer Took 1.296000 and 0.020MB extra memory for 1000000 using each/pairs me +thod

For 5 million keys, for / keys required 228MB of extra memory and so is 40x slower than while / each:

c:\test>each-keys-b -M1=keys -M2=pairs -N=5e6 hash built, starting timer Took 280.350000 and 228.652MB extra memory for 5000000 using keys/pair +s method c:\test>each-keys-b -M1=each -M2=pairs -N=5e6 hash built, starting timer Took 6.613000 and 0.020MB extra memory for 5000000 using each/pairs me +thod

For 8 million keys, for / keys required 340MB of extra memory and so is 80x slower than while / each:

c:\test>each-keys-b -M1=keys -M2=pairs -N=8e6 hash built, starting timer Took 899.040000 and 343.402MB extra memory for 8000000 using keys/pair +s method c:\test>each-keys-b -M1=each -M2=pairs -N=8e6 hash built, starting timer Took 11.112000 and 0.023MB extra memory for 8000000 using each/pairs m +ethod

And at these sizes of hash, my machine has not yet moved into swapping. When I retire tonight, I'll leave the machine running on 10 million keys which will cause swapping and I'll report the timings tomorrow.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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.

In reply to Re^9: Finding the size of a nested hash in a HoH by BrowserUk
in thread Finding the size of a nested hash in a HoH by Jeri

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.