Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
map{} is a wrong idea if the output isn't being used. That is why it can be "slow". But I am hearing that there is no performance penalty for a "void map{}"... meaning that Perl won't create and allocate memory for the output of map{} if is not being used? I didn't know about that. When did this performance enhancement happen?

I think we both agree that a "void map{}" is bad style. I looked at the post from Ratazong and saw the following code being bench marked:

my @sqrt_results = map { sqrt ($_ ) } @input;
This is a perfect thing for map{}: translate one thing to another. map{} can also make 1 to many and many to one translations. In general, I use map{} when the transformation can be expressed as a "one liner+" and foreach() when the code is longer. As a matter of style, this allows me to put foreach(@input) at the top instead of at the end of the program text, compare with: @output = map{lots of lines}@input.

In reply to Re^3: Map Vs Foreach by Marshall
in thread Map Vs Foreach by perlCrazy

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 sharing their wisdom with the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found