Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: map versus for

by dHarry (Abbot)
on Aug 05, 2008 at 07:36 UTC ( #702257=note: print w/ replies, xml ) Need Help??


in reply to map versus for

Instead of replying to every individual response Iíll write a "one-to-many".

First of all thanks monks for the many reactions. Some of them are really useful and give me insight in the tradeoffs involved en when to prefer one over the other. I especially like the responses of dragonchild, Fletch and pc88mxer which I think are the most useful to me.

I can fully appreciate the obvious advantages that map has over for and there is no doubt that using map is usually the better choice. But my eye was drawn to the following two paragraphs:

"There are a couple of other advantages that arenít quite as obvious. For example, when you use map, most of your looping and list generation is being done in heavily optimized compiled C-code, not in interpreted Perl. So itís usually being done considerably faster.

In addition, the map knows in advance exactly how many elements it will eventually process, so it can pre-allocate sufficient space in the list itís returning. Or rather it can usually pre-allocate sufficient space. If the mapís block returns more than one value for each element of the original list, then extra allocations will still be necessary. But, even then, not as many as the equivalent series of push statements would require."

(taken from PBP p11, paragraphs 4 and 5)

I can understand this in no other way then map is usually faster then for. Apparently the example given by Damian doesnít show the difference. It would be interesting to construct an example which shows the advantage in performance for map over for. So far I have not succeeded in doing this.


Comment on Re: map versus for
Re^2: map versus for
by TheDamian (Priest) on Aug 06, 2008 at 00:14 UTC
    ...when you use map, most of your looping and list generation is being done in heavily optimized compiled C-code, not in interpreted Perl. So itís usually being done considerably faster.

    Alas for the static nature of the printed word.

    This assertion used to be true (back when I was first developing PBP in early 2004). I wouldn't have written it if the example I used hadn't confirmed the statement when benchmarked.

    But in all the recent versions of Perl I currently have installed (5.8.3, 5.8.8, 5.10.0), a for outdoes the corresponding map on every test I can think to run. Needless to say, PBP edition 2 will be updated to reflect the current behaviours, but that doesn't fix the thousands of existing copies with that overconfident assertion in them. Sigh. One lives and learns.

    Damian

      Wouldn't this be because of the improvements made in 5.8 re: more efficient aliasing of the loop variable?

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2014-09-20 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls