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
|