Paul-
Why use map without using it's return value? why not two for loops?
Well, I'm just now maturing enough with perl to begin to understand how to think about things with map (look ma, I'm using map!), so naturally I want to show off a bit. However, I also thought map might be more efficient here than 2 for loops (is that correct?).
I couldn't figure out how to use the return value from map to get done what I want. Any pointers here would be great!
Thanks!
-Craig
Update: Oh, you just did... Thanks! | [reply] |
use strict;
use Benchmark qw(cmpthese);
my @a1 = (1 .. 4_000);
cmpthese(4_000, {
for_loops => sub { for(@a1) { $_ = $_ + 1 } },
map_loops => sub { map { $_ = $_ + 1 } @a1 },
});
| [reply] [d/l] [select] |
Actually since 5.8.1 map in void context is optimized to not construct a return list.
(Update: although I'm still get map 7-14% slower no matter how I twiddle your benchmark; scratch that, see below)
Having said that, it's still poor form to use map as a replacement for a proper for loop though (IMHO). Using for says you're iterating over a list of values, whereas using map emphasizes the fact that the point of the iteration is constructing a new list from the existing list (or put another way: for means "I'm walking this list for some reason", whereas map screams "HEY, I WANT TO HAVE A NEW LIST HERE").
Update: Aha, I think I know what the problem is. Try appending a 1; in your sub after the map so that you're explicitly putting the map in void context (since it'll no longer be the last expression in the sub and hence the return value). That makes map a hair (7%) faster for me (5.8.8 on OS X).
Oop: I also changed to use map EXPR, LIST rather than map BLOCK LIST so that's saving me overhead vs for as well.
use strict;
use Benchmark qw(cmpthese);
my @a1 = (1)x4_000;
cmpthese(4_000, {
for_loops => sub { for(@a1) { $_ = $_ + 1 } },
map_loops => sub { map $_ = $_ + 1, @a1; 1 },
});
__END__
Rate for_loops map_loops
for_loops 1724/s -- -6%
map_loops 1826/s 6% --
So you can make it a tiny bit faster (due to being able to "cheat" a little and avoid the enter/leave block overhead), but again I'd stick to using map just for map'ing not general iteration.
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] [select] |