Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Trouble getting size of list returned from sub

by Anonymous Monk
on Nov 26, 2012 at 11:17 UTC ( #1005616=note: print w/ replies, xml ) Need Help??


in reply to Re: Trouble getting size of list returned from sub
in thread Trouble getting size of list returned from sub

Actually they all do that, they copy the list to the "stack", its just that map introduces a scope, by the time it ends, perl has a chance to release the memory it used back to the OS -- an optimization

For a long time map/grep in scalar/void context took double the memory than equivalent for loop, so depending on perl version memory usage will differ between these two lines

warn scalar grep {1} foo(); my $count = 0; $count++ for foo(); warn $count ;

wantarray is what you want to use if you want to be sure :)


Comment on Re^2: Trouble getting size of list returned from sub
Download Code
Re^3: Trouble getting size of list returned from sub
by ColonelPanic (Friar) on Nov 26, 2012 at 12:25 UTC
    Actually, it is not a scoping issue. By that logic, this should also save memory:
    sub get_size { my @a = big_list(); return scalar @a } my $size = get_size();

    However, in a simple test, that uses the same memory as a global array that does the same thing.

    Perl, generally speaking, does not release memory back to the OS unless your system is running out of memory. The memory from lexical variables can be claimed and reused by Perl, but it doesn't go back to the OS.

    All of the other methods created an array in addition to what was copied on the stack. map only creates an array of 1s in addition to what was copied on the stack; thus it uses less memory (unless your original data is no bigger than integers, of course) (and yes, this was not always true in older versions of Perl).



    When's the last time you used duct tape on a duct? --Larry Wall

      Actually, it is not a scoping issue

      Sure it is

      By that logic, this should also save memory:

      No, you're copying the list, same as map used to do, even in void context, instead of merely aliasing it -- @_ is alias until you copy it, but you figured that out yourself in Re: Trouble getting size of list returned from sub

        Sure it is

        I responded to your earlier claim that memory from lexical variables is returned to the OS, based on both a statement from the Perl FAQ and evidence from testing. So, simply continuing to assert your previous position without any new argument is not going to cut it. I would be happy to learn how I am wrong on this, but if I am wrong please explain why.

        No, you're copying the list...

        This appears to be a response to the subroutine solution I posted below. Again, the evidence from testing seems clear enough--it uses less memory than the solutions that explicitly or implicitly make a copy of the array. In this case, my assumption was that the list returned from the first function call is left on the stack and used as the arguments for the second function call. @_ then refers directly to the stack, and thus no new copy is made. Again, I would be happy to be proved wrong on this.

        Note: I'm assuming you're the same Anonymous Monk as the previous one here. My apologies to both of you if that is incorrect.



        When's the last time you used duct tape on a duct? --Larry Wall

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (13)
As of 2014-08-27 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (250 votes), past polls