D
Dr.Ruud
I am investigating what is the fastest way to set up a hash, with the
keys coming from an array, and all values set to a constant like 1.
#!/usr/bin/perl
use warnings ;
use strict ;
use Benchmark qw
hireswallclock cmpthese) ;
my @list = 1..1000 ;
cmpthese( -3, {
mapE => sub { my %a = map(( $_ => 1 ), @list)},
mapB => sub { my %a = map { $_ => 1 } @list },
for => sub { my %a ; $a{ $_ } = 1 for @list },
list => sub { my %a ; @a{ @list } = @list },
range => sub { my %a ; @a{ @list } = 1..@list },
empty => sub { my %a ; @a{ @list } = () },
} ) ;
__END__
Rate mapB mapE list for range empty
mapB 509/s -- -6% -31% -37% -50% -66%
mapE 544/s 7% -- -26% -33% -46% -64%
list 734/s 44% 35% -- -10% -27% -51%
for 812/s 60% 49% 11% -- -20% -46%
range 1012/s 99% 86% 38% 25% -- -32%
empty 1492/s 193% 174% 103% 84% 47% --
From this, I deduce that I need an unlimited supply of ones (in list
context), something like (invalid code follows) C<@1> or C<1...0>.
Suggestions anyone?
keys coming from an array, and all values set to a constant like 1.
#!/usr/bin/perl
use warnings ;
use strict ;
use Benchmark qw
my @list = 1..1000 ;
cmpthese( -3, {
mapE => sub { my %a = map(( $_ => 1 ), @list)},
mapB => sub { my %a = map { $_ => 1 } @list },
for => sub { my %a ; $a{ $_ } = 1 for @list },
list => sub { my %a ; @a{ @list } = @list },
range => sub { my %a ; @a{ @list } = 1..@list },
empty => sub { my %a ; @a{ @list } = () },
} ) ;
__END__
Rate mapB mapE list for range empty
mapB 509/s -- -6% -31% -37% -50% -66%
mapE 544/s 7% -- -26% -33% -46% -64%
list 734/s 44% 35% -- -10% -27% -51%
for 812/s 60% 49% 11% -- -20% -46%
range 1012/s 99% 86% 38% 25% -- -32%
empty 1492/s 193% 174% 103% 84% 47% --
From this, I deduce that I need an unlimited supply of ones (in list
context), something like (invalid code follows) C<@1> or C<1...0>.
Suggestions anyone?