Background
Perldoc for List :: Util suggests that some uses mapcan be replaced with reduceto avoid creating an unnecessary staging list:
For example, to find the total length of all lines in a list, we could use
$total = sum map { length } @strings;
However, this creates a list of temporary integer values, as long as the original list of strings, only to reduce it to one value again. We can more accurately calculate the same result using a block of code that accumulates lengths by writing this instead:
$total = reduce { $a + length $b } 0, @strings;
It makes sense. However, reduceto work in this example, it needs an “identification value” that will be added to the input list:
$total = reduce { $a + length $b } 0, @strings;
, 0, @strings , , creaing map?
($scalar, @list) Perl? ? , :
use strict;
use warnings;
use Benchmark qw/cmpthese/;
my @a1 = 1..10;
my @a2 = 1..100;
my @a3 = 1..1000;
my @a4 = 1..10000;
my @a5 = 1..100000;
my @a6 = 1..1000000;
cmpthese(10000, {
'a1' => sub { my @l = (0, @a1); },
'a2' => sub { my @l = (0, @a2); },
'a3' => sub { my @l = (0, @a3); },
'a4' => sub { my @l = (0, @a4); },
'a5' => sub { my @l = (0, @a5); },
'a6' => sub { my @l = (0, @a6); },
});
:
(warning: too few iterations for a reliable count)
Rate a6 a5 a4 a3 a2 a1
a6 17.6/s -- -90% -99% -100% -100% -100%
a5 185/s 952% -- -90% -99% -100% -100%
a4 1855/s 10438% 902% -- -90% -99% -100%
a3 17857/s 101332% 9545% 862% -- -91% -98%
a2 200000/s 1135940% 107920% 10680% 1020% -- -80%
a1 1000000/s 5680100% 540000% 53800% 5500% 400% --
: (.. 0, @strings ), map reduce?