Skip to content

Instantly share code, notes, and snippets.

@ericychoi
Created February 23, 2017 19:01
Show Gist options
  • Select an option

  • Save ericychoi/ed0d61cbd9860ef391e0bb94fe0c50b0 to your computer and use it in GitHub Desktop.

Select an option

Save ericychoi/ed0d61cbd9860ef391e0bb94fe0c50b0 to your computer and use it in GitHub Desktop.
Perl Benchmark Keys on Hash
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
print "Perl version $]\n";
my %subs;
for my $n (1 .. 5) {
my $m = 10 ** $n;
keys(my %h) = $m; #preallocated the hash so it doesn't have to keep growing
my $k = "a";
%h = ( map { $k++ => 1 } 1 .. $m );
$subs{"10^$n keys"} = sub {
return scalar(keys %h);
}
};
Benchmark::cmpthese -1, \%subs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment