Created
July 22, 2018 17:39
-
-
Save djerius/085fc1dacc14162ad303a5cadc2266a9 to your computer and use it in GitHub Desktop.
benchmark hash element assignment and testing
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
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