Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
You wrote:
> Now I understand that 'keys' would have to > generate a list which might be expensive I > guess, but I'm still a bit puzzled by the results.
Well, you hit the proverbial nail on the head there, though. So I'm not sure there's much to be puzzled about, unless you're just wondering why keys is so slow at generating the list.

But it is that very fact--that keys has to generate a list of the keys each time--that makes it so much slower when breaking early out of the loop. Simply put, you need to do *much* less work when you break early out of the loop when using each, because you haven't had to iterate over the entire hash and build a list.

If, for example, you build the list of keys before you run the benchmark, then in k iterate over that list instead of calling keys anew, you'll find that indeed it is that call that's slowing it down.


In reply to Re: each or keys? by btrott
in thread each or keys? by rdw

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (1)
    As of 2021-04-17 02:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?