Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: 5.8.1 Released!

by dbwiz (Curate)
on Sep 25, 2003 at 13:54 UTC ( [id://294157]=note: print w/replies, xml ) Need Help??


in reply to Re: 5.8.1 Released!
in thread 5.8.1 Released!

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)

Replies are listed 'Best First'.
Re: 5.8.1 Released!
by Abigail-II (Bishop) on Sep 25, 2003 at 14:17 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://294157]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found