Next | Making Programs Faster | 127 |
This gets the keys all at once, in C:
for (keys %hash) { ... }
This gets the keys one at a time, dispatching Perl operations in between:
while (my $k = each %hash) { ... }
The purpose of each is to conserve space, not time
You use it when the hash is very large and you don't want to store all the keys at once
For example if the hash is tied to a large disk file
Since it is a space-conserving optimization, you would expect it to be slower than keys
And so it is
Unless you're also interested in the values
Or unless the keys call causes your program to become memory-bound
Next | Copyright © 2003 M. J. Dominus |