Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: map versus for by dHarry
in thread map versus for by dHarry

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found