in reply to map vs for\foreach.

update

sorry I misunderstood your question, looked like you are comparing $a[$_] = int(rand (100)) for(0..10000000);

/update

for(0..10000000) iterates dynamically but map{...}@a flattens the list which results in shuffling memory...

Better try @a in both cases and the results should level.

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

PS: Je suis Charlie!

Replies are listed 'Best First'.
Re^2: map vs for\foreach.
by builat (Monk) on Mar 11, 2015 at 14:28 UTC
    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
      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.