#! perl -slw use strict; use Benchmark qw[ cmpthese ]; our $SIZE ||= 1000; our $DEBUG; cmpthese( 5, { simple_grep => q[ my @array = 1 .. $SIZE; @array = grep not ($_ % 2), @array; die 'S_G: Mismatch:', scalar @array, " @array[ 0, -1 ]" unless @array == $SIZE / 2 and "@array[ 0, -1 ]" eq "2 $SIZE"; ], splice_for => q[ my @array = 1 .. $SIZE; $array[ $_ ] % 2 and splice @array, $_, 1 for reverse 0 .. $#array; die 'S_F: Mismatch:', scalar @array, " @array[ 0, -1 ]" unless @array == $SIZE / 2 and "@array[ 0, -1 ]" eq "2 $SIZE"; ], move_splice => q[ my @array = 1 .. $SIZE; my( $s, $d ); for( $s = $d = 0; $s < @array; $d++, $s++ ) { $array[ $s ] %2 and $s++, $array[ $d ] = $array[ $s ]; }; $#array = --$d; die 'M_S: Mismatch:', scalar @array, " @array[ 0, -1 ]" unless @array == $SIZE / 2 and "@array[ 0, -1 ]" eq "2 $SIZE"; ], }); __END__ P:\test>371457 -SIZE=100000 (warning: too few iterations for a reliable count) Rate splice_for move_splice simple_grep splice_for 0.798/s -- -94% -94% move_splice 12.3/s 1440% -- -8% simple_grep 13.3/s 1571% 9% -- P:\test>371457 -SIZE=2000000 ## splice_for removed from test. s/iter move_splice simple_grep move_splice 1.66 -- -8% simple_grep 1.52 9% --