in reply to Re^2: Turning foreach into map?
in thread Turning foreach into map?

use strict; use warnings; use Benchmark (); sub t_for_r { my ($url, $list) = @_; my @return; push(@return, "$url/$_\n") foreach @$list; return @return; } sub t_map_r { my ($url, $list) = @_; my @return = map { "$url/$_\n" } @$list; return @return; } sub t_map2_r { my ($url, $list) = @_; return map { "$url/$_\n" } @$list; } sub t_for_a { my $url = shift; my @return; push(@return, "$url/$_\n") foreach @_; return @return; } sub t_map_a { my $url = shift; my @return = map { "$url/$_\n" } @_; return @return; } sub t_map2_a { my $url = shift; return map { "$url/$_\n" } @_; } { my $url = 'http://www.domain.com/'; my @list = qw( file0 file1 file2 file3 file4 file5 file6 file7 file8 file9 ); Benchmark::cmpthese(-3, { t_for_r => sub { $a = join('', t_for_r ($url, \@list)); }, t_map_r => sub { $a = join('', t_map_r ($url, \@list)); }, t_map2_r => sub { $a = join('', t_map2_r($url, \@list)); }, t_for_a => sub { $a = join('', t_for_a ($url, @list)); }, t_map_a => sub { $a = join('', t_map_a ($url, @list)); }, t_map2_a => sub { $a = join('', t_map2_a($url, @list)); }, }); } __END__ Rate t_for_r t_map_r t_for_a t_map_a t_map2_r t_map2_a t_for_r 20641/s -- -3% -5% -5% -33% -36% t_map_r 21196/s 3% -- -2% -2% -31% -34% t_for_a 21651/s 5% 2% -- -0% -29% -33% t_map_a 21684/s 5% 2% 0% -- -29% -32% t_map2_r 30598/s 48% 44% 41% 41% -- -5% t_map2_a 32087/s 55% 51% 48% 48% 5% --

At least for small lists, t_for and t_map are the same, and t_map2 is much faster.

Passing by ref vs passing the array doesn't matter if you use @_ directly.

Replies are listed 'Best First'.
Re^4: Turning foreach into map?
by ysth (Canon) on Apr 04, 2005 at 20:47 UTC
    The other common condition that makes map slower than the equivalent foreach/push is having more output elements than input elements. I'll leave the benchmarking to someone else.
      I believe that I fixed that in Perl 5.6. (I may have the version of Perl wrong.)
        If so, I apologize for the fud. I remember it coming up at least twice, with patches to curb the worst case behaviour, but thought that each time there was criticism, delay, and finally silence. But my memory is not what it could be.
Re^4: Turning foreach into map?
by demerphq (Chancellor) on Apr 04, 2005 at 20:58 UTC

    ID be interested to see the EXPR form of the map statements timed too, iirc they are quicker than the block forms.

    ---
    demerphq

      No difference. I reran it, and it was never more than 3% in favour of either option.

      Rate t_map_b t_map_e t_map_b 28799/s -- -0% t_map_e 28844/s 0% -- This is perl, v5.6.1 built for MSWin32-x86-multi-thread
Re^4: Turning foreach into map?
by Anonymous Monk on Apr 05, 2005 at 09:07 UTC
    Please be aware that the benchmark doesn't show that in this case map is faster than foreach. The fact that t_map2_r and t_map2_a are faster is that the subs don't collect the results into an array, and then return the flattened array, but return the results of the map directly. The overhead of the extra duplication of the result data is more significant than the difference between for and map.