On 8/24/07, Dan Sopher <[EMAIL PROTECTED]> wrote:
> Aside from the syntax, is there a difference in the way 'map' and
> 'foreach' process?
snip

There are many differences, in addition to what has already been said
map can be more or less cpu efficient than depending on the task*.  If
you can use map to avoid  a temporary array, then it can save you some
time, but if you need the intermediate result, then for is faster.
Personally, I prefer map (and its cousin grep) for things that can be
done in one line:

my @files = map { $_->{filename} } $sftp->ls();

rather than

my @files;
push @files, $_->{filename} for $sftp->ls();

But if the code being passed to map becomes to complex, I prefer a for loop:

my @headers;
for my $filename (@filenames) {
    open my $file, "<", $filename
        or die "could not open $filename:$!";
    push @headers, scalar <$file>;
}

rather than

my @headers = map { open my $f, "<", $_ or die "could not open $_:$!";
scalar <$_>; } @filenames;


* Here are the benchmarks for a two different scenarios

Map vs for vs map with temp variable

               Rate map_with_var          for          map
map_with_var 3.51/s           --         -11%         -57%
for          3.92/s          12%           --         -52%
map          8.11/s         131%         107%           --

#!/usr/bin/perl

use strict;
use warnings;
use Benchmark;

my @a = 1 .. 1_000_000;
my %subs = (
        for => sub {
                my @b;
                push @b, ($_ + 4) for @a;
                return @b;
        },
        map => sub {
                return map { $_ + 4 } @a;
        },
        map_with_var => sub {
                my @b = map { $_ + 4 } @a;
                return @b;
        }
);

Benchmark::cmpthese(-1, \%subs);

Map of Map vs Two for loops vs Map of Map with temp variable vs map of
map with two temp variables:

                Rate map_with_2var           for  map_with_var           map
map_with_2var 1793/s            --           -8%          -16%          -45%
for           1950/s            9%            --           -8%          -40%
map_with_var  2124/s           18%            9%            --          -35%
map           3258/s           82%           67%           53%            --

#!/usr/bin/perl

use strict;
use warnings;
use Benchmark;

my @a = 1 .. 1_000;
my %subs = (
        for => sub {
                my @b;
                push @b, ($_ + 4) for @a;
                my @c;
                push @c, ($_ + 4) for @b;
                return @b;
        },
        map => sub {
                return map { $_ + 4 } map { $_ + 4 } @a;
        },
        map_with_var => sub {
                my @b = map { $_ + 4 } map { $_ + 4 } @a;
                return @b;
        },
        map_with_2var => sub {
                my @b = map { $_ + 4 } @a;
                my @c = map { $_ + 4 } @b;
                return @c;
        }
);

Benchmark::cmpthese(-1, \%subs);

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to