Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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 a log crumbles through the grate...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2017-10-19 04:53 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (251 votes). Check out past polls.