Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

by ColonelPanic (Friar)
on Nov 26, 2012 at 09:57 UTC ( #1005604=note: print w/replies, xml ) Need Help??

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

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005604]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-05-23 23:20 GMT
Find Nodes?
    Voting Booth?