Skip to content

Instantly share code, notes, and snippets.

@chernomyrdin
Created November 15, 2012 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chernomyrdin/4077441 to your computer and use it in GitHub Desktop.
Save chernomyrdin/4077441 to your computer and use it in GitHub Desktop.
Get one item from hash
#!/usr/bin/perl -w --
use strict;
use Benchmark;
my %hash;
for (1..1e4) {
my $key = join($_, "*" x (10*rand), (10*rand));
$hash{$key} = "*" x (100*rand);
}
timethese( 1e4, {
'keys' => sub { my ($key) = keys %hash; },
'each' => sub { my ($key) = each %hash; },
} );
__END__
@chernomyrdin
Copy link
Author

Benchmark: timing 10000 iterations of each, keys...
      each:  0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU) @ 1000000.00/s (n=10000)
            (warning: too few iterations for a reliable count)
      keys: 33 wallclock secs (32.90 usr +  0.12 sys = 33.02 CPU) @ 302.85/s (n=10000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment