I changed shell script like this.
perl 025-1.pl each 10000 > log sleep 10; perl 025-1.pl keys 10000 >> log sleep 10; perl 025-1.pl each 100000 >> log sleep 10; perl 025-1.pl keys 100000 >> log sleep 10; perl 025-1.pl each 1000000 >> log sleep 10; perl 025-1.pl keys 1000000 >> log sleep 10; perl 025-1.pl each 2000000 >> log sleep 10; perl 025-1.pl keys 2000000 >> log
And result seems to show me "each" is slower than "keys", especially when hash becomes larger.
###count=10000,each### before : size=8708096 after hash: size=11853824 after loop: size=11853824 time=0.051 ###count=10000,keys### before : size=8708096 after hash: size=11853824 after loop: size=11853824 time=0.043 ###count=100000,each### before : size=8708096 after hash: size=41213952 after loop: size=41213952 time=0.791 ###count=100000,keys### before : size=8708096 after hash: size=41213952 after loop: size=41213952 time=0.680 ###count=1000000,each### before : size=8708096 after hash: size=294969344 after loop: size=296017920 time=8.561 ###count=1000000,keys### before : size=8708096 after hash: size=294969344 after loop: size=296017920 time=7.429 ###count=2000000,each### before : size=8708096 after hash: size=581230592 after loop: size=582279168 time=309.887 ###count=2000000,keys### before : size=8708096 after hash: size=581230592 after loop: size=582279168 time=99.701

My question is
1. Why Gtop says while loop and foreach loop consume exact same memory?
2. Why foreach/keys loop faster in this example? As another person kindly pointed me, Benchmark shows while/each loop is faster.

updated I didn't understand the output of Benchmark... Benchmark also says foreach/keys loop is faster. But question remains. Why?


In reply to Re^8: Finding the size of a nested hash in a HoH by remiah
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.