splice can go to O (n ^ 2) under the conditions you described (since it moves the contents of the array around), and grep / slice will allocate additional O (n) memory (probably much less than 500 GB, but still ...) .
There is a linear solution without additional memory, but it looks more like C than Perl:
sub inplace_grep { my ($code, $array) = @_;
Update: when using grep memory - you can quickly test the additional memory allocation using large amounts of data and is looking for syscall brk . On my system (linux, perl 5.10) it is.
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'
source share