in reply to 5.8.1 Released!

map in void context is no longer expensive. map is now context aware, and will not construct a list if called in void context.

No longer can monks be admonished for employing map in a void context, building a list and discarding it! The faq_monk may need to be updated.

:-)

 

perl -le "print+unpack'N',pack'B32','00000000000000000000001010000111'"

Replies are listed 'Best First'.
Re: Re: 5.8.1 Released!
by dbwiz (Curate) on Sep 25, 2003 at 13:54 UTC

    map in void context is no longer a sin, but still it can't be used lightly instead of for, which is 25% faster than map.

    It has been a great improvement from previous versions, though.

    use Benchmark; print "Perl $]\n"; timethese (10000,{ "map"=> sub { map { $_ } ( 1 .. 1000 ) }, "for"=> sub { $_ for ( 1 .. 1000 ) } } ); __END__ Perl 5.006001 Benchmark: timing 10000 iterations of for, map... for: 3 wallclock secs ( 2.81 usr + 0.00 sys = 2.81 CPU) map: 5 wallclock secs ( 5.16 usr + 0.01 sys = 5.17 CPU) Perl 5.008 Benchmark: timing 10000 iterations of for, map... for: 3 wallclock secs ( 2.19 usr + 0.01 sys = 2.20 CPU) map: 4 wallclock secs ( 4.65 usr + 0.02 sys = 4.67 CPU) Perl 5.008001 Benchmark: timing 10000 iterations of for, map... for: 2 wallclock secs ( 1.72 usr + 0.00 sys = 1.72 CPU) map: 2 wallclock secs ( 2.27 usr + 0.02 sys = 2.29 CPU) Perl 5.008001 Benchmark: timing 50000 iterations of for, map... for: 9 wallclock secs ( 8.71 usr + 0.01 sys = 8.72 CPU) map: 12 wallclock secs (11.53 usr + 0.04 sys = 11.57 CPU)
      Well, now you are comparing map/block with for/expression. Blocks are more expensive than expressions. And while this doesn't mean for isn't slightly faster than map, it does skew the results. Here's another benchmark, where we do a very simple thing in the block/expression:
      #!/usr/bin/perl use strict; use warnings; use Benchmark 'cmpthese'; cmpthese -2 => { map_block => sub {my $s = 0; map {$s +=$_} 1 .. 1000; $s}, for_block => sub {my $s = 0; for (1 .. 1000) {$s += $_}; $s}, map_expr => sub {my $s = 0; map $s += $_ => 1 .. 1000; $s}, for_expr => sub {my $s = 0; $s += $_ for 1 .. 1000; $s}, } __END__ Rate map_block map_expr for_block for_expr map_block 3224/s -- -4% -10% -17% map_expr 3345/s 4% -- -6% -14% for_block 3572/s 11% 7% -- -8% for_expr 3900/s 21% 17% 9% --

      map is slower than for, but the difference is in the same order as the difference between a for/expression and a for/block.

      Abigail

Re: 5.8.1 Released!
by Abigail-II (Bishop) on Sep 25, 2003 at 13:55 UTC
    Yeah, I got that perldelta remark in just in time. ;-)

    Abigail