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


in reply to Using foreach to process a hash

Hi all.

Welcome to the monastery iridius. I took the liberty of including two sources of information you might find beneficial:

DevShed Perl articles

Beginning Perl

The latter is a free online text (written for v5.6.1) that was designed to educate the beginning perl programmer. Feel free to download any chapter you'd like (or all of them).

:)

Concerning your question, jdporter answered it but I felt obligated to contribute. If you're processing a hash of considerable size, obviously you'd prefer to use the most efficient functions possible. In order to accomplish this end, the standard perl library includes a module called Benchmark (please see below for an example):

#!/usr/bin/perl use warnings; use strict; use Benchmark; timethese(1_000_000, { foreach_loop => \&foreach_loop, while_each_loop => \&while_each_loop } ); my %monks = ( jdporter => 'Prior', tye => 'Bishop', bobf => 'Vicar', planetscape => 'Vicar', belg4mit => 'Parson', ); sub foreach_loop { foreach my $key ( keys %monks ) { print "$key => $monks{$key}", "\n"; } } sub while_each_loop { while( my( $key, $value ) = each %monks ) { print "$key => $value", "\n"; } }

Output:



Benchmark: timing 1000000 iterations of foreach_loop, while_each_loop...

foreach_loop: 0 wallclock secs ( 1.16 usr + 0.00 sys = 1.16 CPU) @ 864304.24/s (n=1000000)
while_each_loop: 1 wallclock secs ( 0.51 usr + 0.00 sys = 0.51 CPU) @ 1941747.57/s (n=1000000)

In order to ascertain which function is more efficient, look at the output and you'll see: sys =n.nn CPU. The Benchmark module functions by executing your code as many times as the first parameter after the timethese() subroutine indicates then calculates an average based upon the amount of time it took. It then reports on the total amount of time taken. As one can see, the while_each loop executes faster and that would be a wise choice when iterating over an exceptionally large hash.

Update: In the event you see '(warning: too few iterations for a reliable count)' in the output, simply augment the number of code executions in the timethese() function.

2nd Update: Thanks jdporter and blazar. I rushed through this example and hadn't noticed the small bugs I had introduced by doing so.

Hope this helps,

~Katie