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

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

by wanna_code_perl (Friar)
on Nov 26, 2012 at 09:28 UTC ( [id://1005599]=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

Aha! That makes sense. Thank you (and to the others who replied with similar suggestions). Now, if I do this, can you tell me anything about what's going on under the hood? I.e., is there a performance hit for shoving a 1000's-long list into an anonymous array, and then dereferencing that array to get its length? Or is Perl smart enough to do all of that with one copy of the original list in memory?

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

Replies are listed 'Best First'.
Re^3: Trouble getting size of list returned from sub
by ColonelPanic (Friar) on Nov 26, 2012 at 09:57 UTC

    This does, indeed, create a copy of the array. In fact, in my testing it used more memory than just creating an array variable. Surprisingly (to me, anyway) the temporary variable method was the most memory-efficient.

    However, any option you are going to use incurs a lot of overhead that could be avoided if the sub were written to handle this.

    Here is a script I used to play around with memory usage:

    use Modern::Perl; use Win32::OLE qw/in/; sub memory_usage() { my $objWMI = Win32::OLE->GetObject('winmgmts:\\\\.\\root\\cimv2'); my $processes = $objWMI->ExecQuery("select * from Win32_Process wh +ere ProcessId=$$"); foreach my $proc (in($processes)) { return $proc->{WorkingSetSize}; } } sub big_list {return ('blah')x1_000_000}; #Option 1: memory usage 104,443,904 my $size = scalar @{[big_list()]}; #Option 2: memory usage 99,958,784 #my @arr = big_list(); #my $size = scalar @arr; #Option 3: memory usage 108,441,600 #my $size = () = big_list(); #Comparison: memory usage 11,206,656 (results discarded when not used) +. #big_list(); #my $size = 1; #Can't get what you want, obviously. say "Size: $size"; say 'Memory usage: ', memory_usage(), "\n";
    If you are not on Windows, see this Stackoverflow question, whence I got the memory usage sub, and which also gives some non-Windows options.


    When's the last time you used duct tape on a duct? --Larry Wall
Re^3: Trouble getting size of list returned from sub
by Anonymous Monk on Nov 26, 2012 at 10:27 UTC

    Without wantarray perl doesn't seem to reduce memory usage

    sub mm { print( (`pslist -m $$ 2>NUL`)[-2,-1] )} sub ff { my @fudge = 1 .. 1_000_000; @fudge } sub fa { scalar @{[ &ff ]} } mm; ff; mm; warn fa; __END__ Name Pid VM WS Priv Priv Pk Faults Non +P Page perl 796 62168 46336 44336 48196 12572 +2 34 Name Pid VM WS Priv Priv Pk Faults Non +P Page perl 796 66076 50280 48252 48260 13582 +2 34 1000000 at - line 4.

    Using the accumulator doesn't appear to reduce memory usage, because one function still returns a list

    sub mm { print( (`pslist -m $$ 2>NUL`)[-2,-1] )} sub ff { my @fudge = 1 .. 1_000_000; @fudge } sub fa { scalar( () = &ff ) } mm; ff; mm; warn fa; __END__ Name Pid VM WS Priv Priv Pk Faults Non +P Page perl 1432 62168 46336 44336 48196 12572 +2 34 Name Pid VM WS Priv Priv Pk Faults Non +P Page perl 1432 66076 50280 48252 48260 13582 +2 34 1000000 at - line 4.

    I suppose this could actually be optimized, I see no technical reason, but its fairly minor

      Bah, I fudged it up

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2025-03-21 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (63 votes). Check out past polls.