in reply to When should I use map, for?
So, I might be mistaken here, but:
sub { my @e = map { $_ if (s/0/./g || 1) } @elem }
modifies global @elem in place. The s///g acts on $_, which is an alias to the list item. After the first iteration of mapc, you've altered the sample data. Not sure that matters, as this is a /g it has to inspect all the characters regardless.
As has been mentioned already, the map versions both have two needless logical branches, which taints the results.
A better comparison might be something that doesn't act in-place. Like an addition:
my @b = map { $_ + 2 } @elem; # vs my @b; for ( @elem ) { push @b, $_ + 2; } # -> results: # Rate map for # map 60.3/s -- -23% # for 78.2/s 30% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: When should I use map, for?
by radiantmatrix (Parson) on May 19, 2005 at 21:06 UTC | |
by fishbot_v2 (Chaplain) on May 19, 2005 at 21:18 UTC | |
by shemp (Deacon) on May 19, 2005 at 22:49 UTC | |
by fishbot_v2 (Chaplain) on May 19, 2005 at 22:58 UTC | |
by shemp (Deacon) on May 20, 2005 at 17:03 UTC |