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


in reply to Re: each or keys?
in thread each or keys?

Doh! You're right of course, how silly not to notice it before - the break out test is completely stupid. (I guess in the back of my mind I must have thought the values were coming out in order - which of course they are not). It breaks out very early and that explains why it's so much faster.

If I change the test to >400000 (which gets me about 3/4 through the hash) then I get the following results...

Benchmark: timing 1000 iterations of each, keys...
      each:  2 wallclock secs ( 2.39 usr +  0.00 sys =  2.39 CPU) @ 418.41/s (n=1000)
      keys:  2 wallclock secs ( 2.23 usr +  0.00 sys =  2.23 CPU) @ 448.43/s (n=1000)
      Rate each keys
each 418/s   --  -7%
keys 448/s   7%   --

...so it looks like keys generally wins - because of the 'random' order that things come out, you can't tell how soon a loop test break you out of the loop.

Thanks tilly

rdw