in reply to Re^3: Difference arrays.
in thread Difference arrays.
If the memory use issue is due to thousands of small arrays quadrupling in size, then my solution could be useful.
My point was that it's only useful if it's required for the original arrays to remain unchanged.
I could splice @c (or @a) in the foreach,
That would be extremely slow [ probably O(N2) ] compared to the following [ O(N) ]:
# @c = grep !defined, @c; # is O(N) speed, O(N) memory. # # The following does the same, but # is O(N) speed, O(1) memory. my $last = -1; my $i = -1; while (++$i < @c) { if (defined($c[$i]) { $c[++$last] = $c[$i]; } } $#c = $last;
If the last line isn't O(1) memory, replace it with
pop @c while $#c > $last;
BTW, why do you use a for loop to push the elements of @a onto @c? Why not push @c, @a; instead?
push uses O(N) memory (where N is the number of elements pushed).
for ARRAY (as opposed to for LIST) is optimized to use O(1) memory, I think.
|
|---|