public
Last active

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#!/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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.