#! perl -slw use strict; use Time::HiRes qw[ time ]; sub memUsed { my( $mem ) = `tasklist /nh /fi "PID eq $$"` =~ m[([0-9,]+) K]; $mem =~ tr[,][]d; return $mem / 1024; } our $M1 //= 'each'; our $M2 //= 'pairs'; our $N //= 1e6; my %hash; $hash{ sprintf "KEY%010d", $_ } = $_ for 1 .. $N; my $startMem = memUsed; print "hash built, starting timer"; my $start = time; my $count = 0; if( $M1 eq 'each' ) { if( $M2 eq 'pairs' ) { while( my( $k, $v ) = each %hash ) { ++$count; } } else { while( my $k = each %hash ) { ++$count; } } } else { if( $M2 eq 'pairs' ) { for my $k ( keys %hash ) { my $v = $hash{ $k }; ++$count; } } else { for my $k ( keys %hash ) { ++$count; } } } my $endMem = memUsed; printf "Took %.6f and %.3fMB extra memory for $count using $M1/$M2 method\n" , time() - $start, $endMem - $startMem;