Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: Map Vs Foreach

by Ratazong (Monsignor)
on Nov 26, 2009 at 10:24 UTC ( [id://809523]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Map Vs Foreach
in thread Map Vs Foreach

Some more benchmarking:
my @data = (1..8000000); foreach my $x (@data) { push @arr, ($x+2); } @arr1 = map { $_+ 2 } @data; @arr2 = map { push @arr2, ($_+ 2) } @data;
resulted in the following measurement-output:

Time taken by foreach loop was 2 wallclock secs ( 2.27 usr 0.03 sys + 0.00 cusr 0.00 csys = 2.30 CPU) seconds
Time taken by map block was 4 wallclock secs ( 3.39 usr 0.34 sys + 0.00 cusr 0.00 csys = 3.73 CPU) seconds
Time taken by map block (with push) was 5 wallclock secs ( 4.63 usr 0.05 sys + 0.00 cusr 0.00 csys = 4.67 CPU) seconds


best regards, Rata
(a bit surprised about the performance-differences)(btw.: ActiveState perl 5.8.8)

Update
As Ikegami pointed out correctly (thanks!!), the line with @arr2 is wrong. Changing it to

map { push @arr2, ($_+ 2) } @data;

results in

Time taken by map block (with push) was 3 wallclock secs ( 2.33 usr 0.00 sys + 0.00 cusr 0.00 csys = 2.33 CPU) seconds

being similar fast as the foreach ...

Replies are listed 'Best First'.
Re^5: Map Vs Foreach
by ikegami (Patriarch) on Nov 26, 2009 at 16:16 UTC
    that last line is buggy.
    @arr2 = map { push @arr2, ($_+ 2) } @data;
    should be
    map { push @arr2, ($_+ 2) } @data;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 04:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found