Skip to content

@cowens /gist:151792
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
my %h = map { $_ => $_ } 1 .. 10;
my %subs = (
for => sub {
my @a;
push @a, "$_ $h{$_}\n" for keys %h;
return @a
},
while => sub {
my @a;
while (my ($k, $v) = each %h) {
push @a, "$k $v\n"
};
return @a;
}
);
for my $sub (keys %subs) {
print "$sub => ", $subs{$sub}->(), "\n";
}
for my $n (10, 100, 1_000, 10_000) {
%h = map { $_ => $_ } 1 .. $n;
print "\nfor a hash with $n keys:\n";
Benchmark::cmpthese -1, \%subs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.