http://qs321.pair.com?node_id=36142


in reply to each or keys?

keys just loops over calls to each, but does it somewhat more efficiently than you can in Perl. Which is why when you return the whole list, keys is modestly faster.

But when you leave the loop very early with each, well that is a lot faster than going through all of them! (Hint, you are breaking out after an entry or two.)

UPDATE
My wording confused people. A foreach loop has to build a complete list of values before it does any processing. (Except for a hack for the range operator.) A while loop processes entries as it walks through the list. So the keys approach has to build the whole list even though most will be ignored, the each approach does not. But when you go through all of them, the each approach is doing basically what foreach did up front.

That make sense?