Hello sundialsvc4,

Update: My conclusion was wrong, keys does consume more memory than each. See choroba’s comment and my reply, below.


Is Perl going to build an in-memory anonymous array of all those keys, in order to subsequently foreach through it?

No, Perl uses the same internal iterator in both cases:

So long as a given hash is unmodified you may rely on keys, values and each to repeatedly return the same order as each other.
...
Each hash or array has its own internal iterator, accessed by each, keys, and values.
— quoted from each, but see also keys and values.

Note that this internal iterator (one per hash) is implicitly reset when successive calls to each (or keys, or values) have exhausted the available hash entries. But it can also be reset explicitly, for which the recommended method is to call keys @array in void context (see values).

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^2: Speeding up stalled script by Athanasius
in thread Speeding up stalled script by onlyIDleft

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.