解决方法
splice可以在你描述的条件下(当它移动数组内容时)转到O(n ^ 2),而grep / slice将分配O(n)额外的内存(可能远小于500GB,但仍然……).
有一个没有额外内存的线性解决方案,但看起来更像C,而不像Perl:
sub inplace_grep { my ($code,$array) = @_; # move elements backwards for (my ($to,$from)=(0,0); $from < @$array; $from++) { $code->($array->[$from]) or next; $array->[$to++] = $array->[$from]; }; # remove tail splice @$array,$to; };
更新:关于grep内存使用情况 – 您可以通过使用大量数据并查找brk系统调用来快速测试额外的内存分配.在我的系统(linux,perl 5.10)上它确实如此.
strace -e trace=brk perl -MTime::HiRes -wle \ 'print "start ".time; my @array = 1..10**7; print "alloc ".time; @array = grep { $_ %2 } @array; print "grep ".time'