![]() |
|
Do you know where your variables are? | |
PerlMonks |
Re^2: keys and values order on a hashby Somni (Friar) |
on Jul 11, 2011 at 18:22 UTC ( #913743=note: print w/replies, xml ) | Need Help?? |
each has its own problems in terms of maintainability. Bailing out of the loop early (perhaps an exception was hit, perhaps there was a return or last) will leave the iterator in the middle of the hash.
This can lead to some subtle hard to trace bugs. I've been personally bitten by this several times, and I've essentially dropped each from my toolkit; it's simply too easy to leave the iterator in a strange spot. I also wouldn't be too sure of your optimization suggestion. With a pretty basic benchmark I slapped together (source here) it would appear separate keys/values is faster:
This is with a Debian Lenny perl 5.10.1. I'm speculating the overhead of the multiple each calls is killing any gains you get from not iterating a second time; that, or the iteration is cheap because of how the HV is built.
In Section
Seekers of Perl Wisdom
|
|