Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: map vs for\foreach.

by builat (Monk)
on Mar 11, 2015 at 14:28 UTC ( [id://1119639]=note: print w/replies, xml ) Need Help??


in reply to Re: map vs for\foreach.
in thread map vs for\foreach.

You right. But..
foreach(@a){$_+=1;}; real 0m3.756s user 0m3.588s sys 0m0.120s

map{$_+= 1} @a; real 0m5.233s user 0m5.056s sys 0m0.164s

Replies are listed 'Best First'.
Re^3: map vs for\foreach.
by LanX (Saint) on Mar 11, 2015 at 14:36 UTC
    Sorry I misunderstood your question (see update in meantime), but it can still be the same effect that for tries to be clever and iterates @a, while map is flattening.

    Output with perl -MO=Terse seems to indicate this...

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

      for (@a) is indeed optimized to not place the elements of `@a` on the stack. for ((),@a) would prevent the optimization.
      Thx. For this info. It new for me.
        > It new for me.

        Not sure if I was able to help you, this area of implementation details is a moving target.

        And the margins you benchmarked do not really justify any micro-optimisations here, because different Perl versions can behave differently.

        Personally I'd rather prefer for to alter an array via alias, such side-effects in map might confuse other readers (a map in void context used to be a matter of warning)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        PS: Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found