in reply to Re^2: Scanning a list for indices
in thread Scanning a list for indices

Nope, worse. The map still loops just like a for loop would have to, but it also has to manipulate an argument stack (or something like it) so that it can set the $_ and then return it. I'm really not very sure about what I just said, but it is my understanding that is the case.

The only way to know for sure it so try it.

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 }, });
Rate map_loops for_loops map_loops 499/s -- -40% for_loops 835/s 67% --

-Paul

Replies are listed 'Best First'.
Re^4: Scanning a list for indices
by Fletch (Bishop) on Feb 13, 2008 at 15:32 UTC

    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.

      I'm fascinated by the void-map optimization. Computer language construction is a hobby of mine and I've spent many hours reading through the perl sources. I think it's probably a fairer [is that a word] comparison to use the BLOCK form of map, since that's more directly similar to the for loop. Suppose you were doing more than just adding one... suppose it was several commands.

      Also, thanks. Your post rocks.

      -Paul