One issue is that the result is clouded by an extra bogus match. Only one of the 'tail' matches is required. Adding the second one is really bad news for the single regex variant. Choosing to 'nibble' (\s) instead of swallowing whole (\s+) does the single regex a huge disservice too. Consider the following benchmark:
Rate sExNibble singleNibble mExNibble multi multiNib +ble single sExNibble 1795/s -- -74% -99% -99% - +99% -99% singleNibble 6881/s 283% -- -95% -95% - +97% -98% mExNibble 134665/s 7403% 1857% -- -5% - +49% -60% multi 142315/s 7830% 1968% 6% -- - +46% -58% multiNibble 263146/s 14562% 3724% 95% 85% + -- -22% single 338640/s 18769% 4822% 151% 138% +29% --
use strict; use warnings; use Benchmark qw(cmpthese); my $testStr = (' ' x 1000) . 'x' . (' ' x 1000); cmpthese ( -1, { sExNibble => "sExNibbleRegex ('$testStr');", singleNibble => "singleNibbleRegex ('$testStr');", single => "singleRegex ('$testStr');", mExNibble => "mExNibbleRegex ('$testStr');", multiNibble => "multiNibbleRegex ('$testStr');", multi => "multiRegex ('$testStr');", } ); sub sExNibbleRegex { my ($string) = @_; $string =~ s/^\s|\s$|\s+$//g; } sub singleNibbleRegex { my ($string) = @_; $string =~ s/^\s|\s$//g; } sub singleRegex { my ($string) = @_; $string =~ s/^\s+|\s+$//g; } sub multiNibbleRegex { my ($string) = @_; $string =~ s/\^s//g; $string =~ s/\s$//g; } sub mExNibbleRegex { my ($string) = @_; $string =~ s/\^s//g; $string =~ s/\s$//g; $string =~ s/\s+$//g; } sub multiRegex { my ($string) = @_; $string =~ s/\^s+//g; $string =~ s/\s+$//g; }
In reply to Re: question about reg exp engine
by GrandFather
in thread question about reg exp engine
by chuckd
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |