in reply to does splice'ing an array of hashes free memory?

Yes, but splicing isn't a cheap operation. In fact, if @cvs_to_delete contains x% of the elemens of @cvs (for arbitrary x), splicing them out 1 by 1 has an expected quadratic running time.

It's better to use a slice:

my %cvs_to_delete = map {($_, 1)} @cvs_to_delete; @cvs = @cvs[grep {!$cvs_to_delete{$_}} 0..$#cvs];

Replies are listed 'Best First'.
Re^2: does splice'ing an array of hashes free memory?
by lagle (Novice) on Nov 01, 2010 at 16:36 UTC

    you're right that it's expensive. but the grep method is even slower, because the arrays are ~30000 long, and the information i "extract" from them is somewhere near the beginning of the array and their length is somewhere between 1-15. the grep method iterates to the end of the array which is what i try to prevent

    my first version just splice'd it the amount of $#cvs_to_delete, it ran quick, but i can't be sure it works.

    i'll try to make something that splices all consequtive rows in one sweep.

      here's the code i ended up for splicing subsequent indexes in one go. i have to say it saves me literally hours.

      # now we'll pop away what we found from the AoH # basically we splice out subsequent indexes in one go # and the poor lonelies have to go home alone # if nothing is to be popped, we don't pop unless (scalar @i_to_delete) { return $text; } @i_to_delete = sort {$b <=> $a} @i_to_delete; # descending order my $length = 1; my $offset = undef; for my $i (0 .. $#i_to_delete) { if ($i < $#i_to_delete) { # not the last index if (($i_to_delete[$i] - $i_to_delete[$i+1]) == 1) { # the two +are subsequent $length++; } else { # they are not subsequent, split current $offset = $i_to_delete[$i]; splice(@$AoH, $offset, $length); $length = 1; } } else { # last index $offset = $i_to_delete[$i]; splice(@$AoH, $offset, $length); $length = 1; } }