#! /usr/bin/perl -w use Benchmark qw(cmpthese); use strict; use integer; my %hash; for (my $i = 0; $i < 1000; $i++) { $hash{$i} = $i; } sub e { my $total = 0; keys %hash; while (my($k, $v) = each %hash) { $total += $v; last if $total > 500; } } sub k { my $total = 0; foreach my $k (keys %hash) { my $v = $hash{$k}; $total += $v; last if $total > 500; } } cmpthese(100000, { 'each' => \&e, 'keys' => \&k });