Created
February 23, 2017 19:01
-
-
Save ericychoi/ed0d61cbd9860ef391e0bb94fe0c50b0 to your computer and use it in GitHub Desktop.
Perl Benchmark Keys on Hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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