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