: Because someone (and with apologies to all, I don't recall off the top
: of my head who)correctly pointed out to me earlier in this thread that
: using map() here was inefficient. map() builds and returns an array, so
: there's no point in using it in this void context. Aside from that,
: both do the same thing. The postfix for is cleaner. =o)
I agree that the postfix is cleaner, but when I benchmark these, map
looks faster- though several months ago, map was slower (IIRC). Maybe
something changed in 5.6.0 to make map faster in a null context...?
Here's the script & output (Perl 5.6.0 on an Ultra 10):
archdev 10:58% more ./z
#!/archive/data1/bin/perl
use strict;
use Benchmark;
my @lines = qw(
10th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th
a AAA AAAS Aarhus Aaron AAU ABA Ababa aback abacus
);
timethese(500_000,{
"1. map " => 'map { s/a// } @lines',
"2. foreach" => 'foreach ( @lines ) { s/a// }',
"3. for " => 's/a// for @lines',
});
archdev 10:58am 147% ./z
Benchmark: timing 500000 iterations of 1. map , 2. foreach, 3. for ...
1. map : 0 wallclock secs ( 0.93 usr + 0.00 sys = 0.93 CPU) @ 537634.41/s
(n=500000)
2. foreach: 2 wallclock secs ( 2.29 usr + 0.00 sys = 2.29 CPU) @ 218340.61/s
(n=500000)
3. for : 1 wallclock secs ( 2.32 usr + 0.00 sys = 2.32 CPU) @ 215517.24/s
(n=500000)
-- tdk