Skip to content

Instantly share code, notes, and snippets.

@djerius
Created July 22, 2018 17:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djerius/085fc1dacc14162ad303a5cadc2266a9 to your computer and use it in GitHub Desktop.
Save djerius/085fc1dacc14162ad303a5cadc2266a9 to your computer and use it in GitHub Desktop.
benchmark hash element assignment and testing
use Benchmark ':all';
my @array = 1 .. 1000;
cmpthese(
10000,
{
'%foo = map { $_ => 1 } @array' => sub {
my %foo = map { $_ => 1 } @array;
},
'@foo{@array} = undef' => sub {
my %foo;
@foo{@array} = undef;
},
'@foo{@array} = (1) x @array' => sub {
my %foo;
@foo{@array} = (1) x @array;
},
'@foo{@array} = @array' => sub {
my %foo;
@foo{@array} = @array;
},
},
);
my %foo = (1) x @array;
cmpthese(
100000,
{
'grep { exists $foo{_} } @array' => sub {
my @foo = grep { exists $foo{_} } @array;
},
'grep { $_ } @foo{@array}' => sub {
my @foo = grep { $_ } @foo{@array};
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment