kroach has asked for the wisdom of the Perl Monks concerning the following question:
I'm using the $# feature to shrink an array. After shrinking to half the size, the array seems to take a bit more memory. How is that possible? (perl 5.10 on 64-bit GNU/Linux)
Output:use strict; use warnings; use feature qw(say); use Devel::Size qw(size); my @array = 1 .. 10_000; say size(\@array); $#array = 5_000; say size(\@array);
80064 80216
Is there a way to shrink the array without causing it to grow (and probably be reallocated)?
UPDATE: It seems splice doesn't cause the same effect.
my @array = 1 .. 10_000; say size(\@array); splice @array, 5_001; say size(\@array);
Output:
80064 80064
Furthermore, after running a few simple benchmarks, it turns out the splice method is about as fast as last index assignment, so it seems splice doesn't do unnecessary copying in void context.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Shrunk array takes more memory than original
by BrowserUk (Patriarch) on Nov 10, 2015 at 13:04 UTC | |
Re: Shrunk array takes more memory than original
by choroba (Cardinal) on Nov 10, 2015 at 12:26 UTC | |
by kroach (Pilgrim) on Nov 10, 2015 at 12:45 UTC | |
by BrowserUk (Patriarch) on Nov 10, 2015 at 13:06 UTC | |
by dave_the_m (Monsignor) on Nov 10, 2015 at 13:12 UTC | |
by BrowserUk (Patriarch) on Nov 10, 2015 at 13:16 UTC |
Back to
Seekers of Perl Wisdom