Skip to content

Instantly share code, notes, and snippets.

@savonarola
Forked from anonymous/b.pl
Created July 26, 2010 11:03
Show Gist options
  • Save savonarola/490427 to your computer and use it in GitHub Desktop.
Save savonarola/490427 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
use strict;
use Benchmark;
my $h = { map { $_, $_ } 1..10000 };
timethese( 1000, {
each => sub{ my $i = 0; while( my ($k, $v) = each %$h ) { $i++ } },
values => sub{ my $i = 0; foreach my $v (values %$h) { $i++ } },
});
__END__
Benchmark: timing 1000 iterations of each, values...
each: 10 wallclock secs ( 9.75 usr + 0.01 sys = 9.76 CPU) @ 102.46/s (n=1000)
values: 2 wallclock secs ( 1.98 usr + 0.00 sys = 1.98 CPU) @ 505.05/s (n=1000)+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment