use 5.020; use warnings; use Benchmark qw( cmpthese ); use Getopt::Long; my ( $times, $size ) = ( 1_000, 1_000 ); GetOptions( 'times=i' => \$times, 'size=i' => \$size, ); my @keys = 'a' .. 'z'; my @vals = map $keys[ rand @keys ], 1 .. $size; sub with_undef { my @needles = @keys; my @haystack = @vals; for my $needle (@needles) { for my $elem (@haystack) { next if not defined $elem; if ( $elem eq $needle ) { $elem = undef; } } } } sub with_splice { my @needles = @keys; my @haystack = @vals; for my $needle (@needles) { for my $idx ( 0 .. $#haystack ) { last if $idx > $#haystack; if ( $haystack[$idx] eq $needle ) { splice @haystack, $idx, 1; redo; } } } } cmpthese( $times, { undef => \&with_undef, splice => \&with_splice, } );