use Benchmark 'cmpthese';
use strict;
use Regexp::Keep;
my @strings = ('one two three four', 'two three four five', 'one three five');
my @copy;
sub replace { s/(o+ )three/$1/ for @copy=@strings }
sub keep { s/o+ \Kthree// for @copy=@strings }
sub two { /o+ (?=three)/g and s/\Gthree// for @copy=@strings }
cmpthese(-3, {
'replace' => \&replace,
'keep' => \&keep,
'two' => \&two
});
replace;
print "Replace: ", join("\n", @copy), "\n";
keep;
print "Keep: ", join("\n", @copy), "\n";
two;
print "Two: ", join("\n", @copy), "\n";
####
Rate replace keep two
replace 4739/s -- -15% -26%
keep 5587/s 18% -- -13%
two 6386/s 35% 14% --
Replace: one two four
two four five
one three five
Keep: one two four
two four five
one three five
Two: one two four
two four five
one three five
####
sub replace { s/(o+ )three/$1/g for @copy=@strings }
sub keep { s/o+ \Kthree//g for @copy=@strings }
sub two { do {s/\Gthree// while /o+ /g} for @copy=@strings }
####
sub two { /o+ (?=(three))/g and substr($_, $-[1], $+[1]-$-[1], '') for @copy=@strings