my $idx = 0; for my $j (@a) { if ($j == $target) { splice(@a, $idx, 1); last; } ++$idx; } #### my $idx = firstidx { $_ == $target } @a; splice(@a, $idx, 1) if $idx >= 0; #### Rate for firstidx for 24.3/s -- -22% firstidx 31.1/s 28% -- #### #!/usr/bin/env perl use 5.012; use warnings; use Benchmark qw/cmpthese/; use List::Util qw/shuffle/; use List::MoreUtils qw/firstidx/; my @for = 1..1e6; my @firstidx = 1..1e6; my @remove_for = shuffle @for; my @remove_firstidx = @remove_for; # Same search order # Use iteration count, not time, so we can verify results. cmpthese(100, { for => sub { my $target = pop @remove_for; my $idx = 0; for my $j (@for) { if ($j == $target) { splice(@for, $idx, 1); last; } ++$idx; } }, firstidx => sub { my $target = pop @remove_firstidx; my $idx = firstidx { $_ == $target } @firstidx; splice(@firstidx, $idx, 1) if $idx >= 0; }, }); # Sanity check: arrays are the same die "Array length mismatch" if @for != @firstidx; die "Content different" if grep { $for[$_] != $firstidx[$_] } 0..$#for;