note
blahblahblah
Here's another attempt, with code that actually solves the problem of stripping of trailing whitespace.
<code>
use Benchmark 'cmpthese';
my $which = $ARGV[0] || 'short';
my $str = {'short' => "a very short one",
'long' => "alphabet X alphabet" x 100 . "junk at the end" x 10,
'shortspaces' => " asdfasdf fdsdsf a sdfa sdf 3432 324 ",
}->{$which};
my $duration = 0;
my $verify = 0;
if ($ARGV[1] eq 'verify')
{
$duration = 1;
$verify = 1;
}
cmpthese($duration, {
last => sub { my $x = $str; $x =~ s/\s*$//; $method = "last"; print "$x|\n" if $verify; },
plus => sub { my $x = $str; $x =~ s/\s+$//; $method = "plus"; print "$x|\n" if $verify; },
rx_rx => sub { my $x = $str; $x =~ /.*\S/g and $x =~ s/\G.*//; $method = "rxrx"; print "$x|\n" if $verify; },
sexeger => sub { my $x = $str; ($x = reverse $x) =~ s/^\s+//; $x = reverse $x; $method = "sgxr"; print "$x|\n" if $verify; },
lookbehind => sub { my $x = $str; $x =~ s/.\K\s+$//; $method = "lkbd"; print "$x|\n" if $verify; },
detectregex => sub { my $x = $str; if ($x =~ /\s/){($x = reverse $x) =~ s/^\s+//; $x = reverse $x;} $method = "dtrs"; print "$x|\n" if $verify; },
detectsubstr => sub { my $x = $str; if (substr($x, -1) =~ /^\s/){($x = reverse $x) =~ s/^\s+//; $x = reverse $x;} $method = "dtss"; print "$x|\n" if $verify;\
},
});
__END__
./test long ; ./test short ; ./test shortspaces
Rate lookbehind last plus detectregex sexeger rx_rx detectsubstr
lookbehind 750/s -- -5% -86% -97% -97% -99% -100%
last 792/s 6% -- -85% -96% -97% -99% -100%
plus 5322/s 610% 572% -- -76% -77% -92% -98%
detectregex 21914/s 2823% 2666% 312% -- -6% -68% -93%
sexeger 23287/s 3006% 2839% 338% 6% -- -66% -92%
rx_rx 67557/s 8910% 8426% 1169% 208% 190% -- -78%
detectsubstr 303483/s 40373% 38201% 5603% 1285% 1203% 349% --
Rate lookbehind last detectregex rx_rx plus sexeger detectsubstr
lookbehind 81153/s -- -1% -54% -59% -67% -71% -83%
last 81683/s 1% -- -54% -58% -66% -71% -83%
detectregex 178253/s 120% 118% -- -9% -27% -37% -63%
rx_rx 196768/s 142% 141% 10% -- -19% -30% -59%
plus 243234/s 200% 198% 36% 24% -- -14% -50%
sexeger 282994/s 249% 246% 59% 44% 16% -- -41%
detectsubstr 481782/s 494% 490% 170% 145% 98% 70% --
Rate lookbehind last plus detectregex detectsubstr rx_rx sexeger
lookbehind 35057/s -- -9% -72% -76% -77% -80% -83%
last 38419/s 10% -- -69% -74% -74% -78% -82%
plus 123667/s 253% 222% -- -15% -18% -30% -41%
detectregex 145442/s 315% 279% 18% -- -3% -18% -31%
detectsubstr 149917/s 328% 290% 21% 3% -- -15% -29%
rx_rx 176362/s 403% 359% 43% 21% 18% -- -16%
sexeger 210526/s 501% 448% 70% 45% 40% 19% --
</code>
Using substr to check the last char really helps for the long strings; the others are not so decisive. For every case, my current method of blindly doing s/\s*$/ is the worst. Any suggestions for making it faster? Thanks.
782077
782077