Randal L. Schwartz wrote:

>>>>>> "Peter" == Peter Farrar <[EMAIL PROTECTED]> writes:
> 
>>> Replace
>>> 
>>> EVIL: map { some;block;of;code;that;changes;$_ } @some_array;
>>> 
>>> with
>>> 
>>> GOOD: for (@some_array) { some;block;of;code;that;changes;$_ }
> 
> 
> The foreach loop *will* be faster.
> 

for a small array, map is probably faster than foreach or for:

#!/usr/bin/perl -w
use strict;
use Benchmark;

timethese(999999,{_map=>sub{map{1} (1..10)},
                  _for=>sub{for(1..10){1}},
                 _foreach=>sub{foreach(1..10){1}}});
__END__

prints:

Benchmark: timing 999999 iterations of _for, _foreach, _map...
      _for: 29 wallclock secs (22.80 usr +  0.02 sys = 22.82 CPU) @ 
43821.17/s (n=999999)
  _foreach: 27 wallclock secs (23.01 usr +  0.04 sys = 23.05 CPU) @ 
43383.90/s (n=999999)
      _map: 18 wallclock secs (18.39 usr +  0.01 sys = 18.40 CPU) @ 
54347.77/s (n=999999)

for a larger array(the following is done with array size of 100):

Benchmark: timing 99999 iterations of _for, _foreach, _map...
      _for:  9 wallclock secs ( 8.15 usr +  0.00 sys =  8.15 CPU) @ 
12269.82/s (n=99999)
  _foreach:  7 wallclock secs ( 8.16 usr +  0.00 sys =  8.16 CPU) @ 
12254.78/s (n=99999)
      _map: 20 wallclock secs (14.77 usr +  0.00 sys = 14.77 CPU) @ 
6770.41/s (n=99999)

map is slow.

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to