Rate sExNibble singleNibble mExNibble multi multiNibble 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; }