C:\test>1036622 -N=1e3
Rate grep build_new_array offset_copy for_splice edit_in_place
grep 678/s -- -22% -27% -34% -41%
build_new_array 870/s 28% -- -6% -15% -24%
offset_copy 929/s 37% 7% -- -9% -19%
for_splice 1024/s 51% 18% 10% -- -11%
edit_in_place 1150/s 70% 32% 24% 12% --
C:\test>1036622 -N=1e4
Rate for_splice grep offset_copy build_new_array edit_in_place
for_splice 71.5/s -- -9% -11% -22% -39%
grep 78.7/s 10% -- -2% -14% -33%
offset_copy 80.6/s 13% 2% -- -12% -31%
build_new_array 91.3/s 28% 16% 13% -- -22%
edit_in_place 117/s 64% 49% 46% 29% --
C:\test>1036622 -N=1e5
Rate for_splice grep build_new_array offset_copy edit_in_place
for_splice 1.29/s -- -75% -83% -84% -85%
grep 5.12/s 296% -- -33% -35% -42%
build_new_array 7.68/s 494% 50% -- -3% -14%
offset_copy 7.88/s 509% 54% 3% -- -11%
edit_in_place 8.89/s 587% 74% 16% 13% --
C:\test>1036622 -N=1e6
s/iter for_splice grep build_new_array offset_copy edit_in_place
for_splice 77.9 -- -98% -98% -99% -99%
grep 1.39 5503% -- -14% -18% -38%
build_new_array 1.20 6379% 16% -- -5% -28%
offset_copy 1.14 6737% 22% 6% -- -24%
edit_in_place 0.868 8884% 60% 39% 31% --
####
:
#! perl -slw
use strict;
use Benchmark qw[ cmpthese ];
our $N //= 1e3;
our $I //= -1;
our @as;
@{ $as[ $_ ] } = 1 .. $N for 0 .. 4;
cmpthese $I, {
for_splice => q[
my $ar = $as[0];
$ar->[$_] =~ /9/ and splice @$ar, $_, 1 for reverse 0 .. $#$ar;
$I == 1 and print "0: ", "@$ar";
],
grep => q[
my $ar = $as[1];
@$ar = grep !/9/, @$ar;
$I == 1 and print "1: ", "@$ar";
],
offset_copy => q[
my $ar = $as[2];
my $o = 0;
for( 0 .. $#$ar ) {
$ar->[$_-$o] = $ar->[$_];
$ar->[ $_ ] =~ /9/ and ++$o;
}
$#$ar = $#$ar - $o;
$I == 1 and print "2: ", "@$ar";
],
build_new_array=>q[
my $ar = $as[3];
my @b;
for( @$ar ) {
push @b, $_ unless /9/;
}
$I == 1 and print "3: ", "@b";
],
edit_in_place=>q[
my $ar = $as[4];
my $o = 0;
for( @$ar ) {
$ar->[$o++] = $_ unless /9/;
}
$#$ar = $o - 1;
$I == 1 and print "4: ", "@$ar";
],
};
####
C:\test>1036622 -N=1e3
Rate grep build_new_array offset_copy edit_in_place for_splice
grep 1464/s -- -20% -43% -61% -65%
build_new_array 1820/s 24% -- -29% -52% -56%
offset_copy 2578/s 76% 42% -- -32% -38%
edit_in_place 3803/s 160% 109% 48% -- -8%
for_splice 4144/s 183% 128% 61% 9% --
C:\test>1036622 -N=1e4
(warning: too few iterations for a reliable count)
Rate grep build_new_array offset_copy edit_in_place for_splice
grep 159/s -- -25% -44% -62% -66%
build_new_array 212/s 33% -- -26% -49% -54%
offset_copy 287/s 80% 36% -- -31% -38%
edit_in_place 419/s 163% 98% 46% -- -10%
for_splice 464/s 191% 119% 62% 11% --
C:\test>1036622 -N=1e5
Rate grep build_new_array offset_copy for_splice edit_in_place
grep 15.4/s -- -22% -50% -69% -70%
build_new_array 19.8/s 28% -- -36% -60% -62%
offset_copy 31.0/s 102% 57% -- -37% -40%
for_splice 49.6/s 222% 151% 60% -- -4%
edit_in_place 51.6/s 235% 161% 66% 4% --
C:\test>1036622 -N=1e6
Rate grep build_new_array offset_copy edit_in_place for_splice
grep 1.60/s -- -23% -51% -70% -71%
build_new_array 2.06/s 29% -- -37% -61% -62%
offset_copy 3.28/s 105% 59% -- -38% -40%
edit_in_place 5.26/s 229% 155% 60% -- -4%
for_splice 5.49/s 243% 166% 67% 4% --