Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save ericychoi/d9038a2c7de5aa21d3f388c1447e9624 to your computer and use it in GitHub Desktop.
Perl Benchmark on Keys (with a wrapper function)
#!/opt/perl/bin/perl
use strict;
use warnings;
use Benchmark;
# Perl version 5.010001
# Rate 10^4 keys 10^3 keys 10^5 keys 10^1 keys 10^2 keys
# 10^4 keys 2429401/s -- -0% -1% -2% -2%
# 10^3 keys 2429401/s 0% -- -1% -2% -2%
# 10^5 keys 2453219/s 1% 1% -- -1% -1%
# 10^1 keys 2477508/s 2% 2% 1% -- -1%
# 10^2 keys 2490475/s 3% 3% 2% 1% --
print "Perl version $]\n";
my %subs;
sub getKeys {
my $hh = shift;
return keys(%$hh);
}
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);
return scalar(getKeys(\%h));
}
};
Benchmark::cmpthese -1, \%subs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment