Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
"be consistent"
 
PerlMonks  

Re^2: Efficiency of map vs. more verbose basic/fundamental code

by aaron_baugher (Deacon)
on Oct 05, 2012 at 11:58 UTC ( #997455=note: print w/ replies, xml ) Need Help??


in reply to Re: Efficiency of map vs. more verbose basic/fundamental code
in thread Efficiency of map vs. more verbose basic/fundamental code

I find statement modifiers to be the clearest way to write code, but maybe I'm just used to them. Still, I'm having trouble seeing why turning a standard for loop into a statement modifier cuts down on the memory usage. Why are examples A and B below different? Is it that the parentheses cause a list to be created in memory? Would example C be as bad as A?

# A for ( sort keys %h ){ dostuffwith($_); } # B dostuffwith($_) for sort keys %h; # C dostuffwith($_) for (sort keys %h);

I guess I need to learn to benchmark memory usage.

Aaron B.
Available for small or large Perl jobs; see my home node.


Comment on Re^2: Efficiency of map vs. more verbose basic/fundamental code
Download Code
Re^3: Efficiency of map vs. more verbose basic/fundamental code
by BrowserUk (Pope) on Oct 05, 2012 at 12:21 UTC
    Still, I'm having trouble seeing why turning a standard for loop into a statement modifier cuts down on the memory usage.

    I didn't suggest it did.

    Only that it reduced the memory requirement compared to the map variant; and sourcecode/mindspace compared to the standard for loop.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      Ah, my misunderstanding, I see that you didn't suggest what I was assuming. I agree; the statement modifier form uses less mindspace for me too.

      Aaron B.
      Available for small or large Perl jobs; see my home node.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-04-20 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls