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

Re: map versus for

by GrandFather (Saint)
on Aug 05, 2008 at 11:41 UTC ( [id://702330]=note: print w/replies, xml ) Need Help??


in reply to map versus for

Well, it all depends on what you do with the results. Consider the following benchmark code:

use strict; use warnings; use Benchmark qw(cmpthese); my @source = 1 .. 1000000; my @result = @source; cmpthese (-3, { for => '@result = test_for ()', map => '@result = test_map ()', mapf => '@result = test_mapf ()', forv => 'test_for ()', mapv => 'test_mapf ()', }); sub test_for { my @sqrt_results; for my $result (@source) { push @sqrt_results , sqrt($result); } return @sqrt_results; } sub test_map { return my @sqrt_results = map { sqrt $_ } @source; } sub test_mapf { return map { sqrt $_ } @source; }

Prints:

Rate map for mapf forv mapv map 2.24/s -- -3% -31% -49% -78% for 2.31/s 3% -- -29% -48% -77% mapf 3.27/s 46% 41% -- -26% -68% forv 4.43/s 98% 92% 36% -- -56% mapv 10.1/s 353% 339% 210% 129% --

Update: and for about 100,000 or fewer elements percentages are:

Rate for map mapf forv mapv for 23849/s -- -3% -29% -46% -76% map 24506/s 3% -- -27% -45% -76% mapf 33666/s 41% 37% -- -24% -66% forv 44571/s 87% 82% 32% -- -56% mapv 100159/s 320% 309% 198% 125% --

In this case for my @source = 1 .. 100;, but the percentages are very much the same over a wide range of elements.


Perl reduces RSI - it saves typing

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found