One for largish and one for small lists:
$ perl -MBenchmark -e'timethese(10000, { map => sub { @_ = map 1, 1..1
+000 }, x => sub { @_ = (1)x1000 }})'
Benchmark: timing 10000 iterations of map, x...
map: 29 wallclock secs (27.09 usr + 0.24 sys = 27.33 CPU) @ 36
+5.90/s (n=10000)
x: 14 wallclock secs (13.36 usr + 0.09 sys = 13.45 CPU) @ 74
+3.49/s (n=10000)
$ perl -MBenchmark -e'timethese(1000000, { map => sub { @_ = map 1, 1.
+.5 }, x => sub { @_ = (1)x5 }})'
Benchmark: timing 1000000 iterations of map, x...
map: 17 wallclock secs (15.55 usr + 0.15 sys = 15.70 CPU) @ 63
+694.27/s (n=1000000)
x: 9 wallclock secs ( 8.44 usr + 0.11 sys = 8.55 CPU) @ 11
+6959.06/s (n=1000000)
The message is clear - a pretty precise 100% speed benefit from x. Of note is what happens internally:
$ perl -MO=Deparse,-x7 -e'timethese(1000000, { map => sub { @_ = map 1
+, 1..5 }, x => sub { @_ = (1)x5 }})'
timethese 1000000, {'map', sub {
@_ = map(1, (1, 2, 3, 4, 5));
}
, 'x', sub {
@_ = (1) x 5;
}
};
-e syntax OK
I'll spare you the corresponding output from 1..1000 - a list of 1000 numeric literals. In other words not only does the map take twice as long in every instance, it also consumes more memory - grossly so, for large lists.
Makeshifts last the longest. |