Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How do I quickly strip blank space from the beginning/end of a string?

by blahblahblah (Priest)
on Jul 22, 2009 at 00:51 UTC ( [id://782134]=note: print w/replies, xml ) Need Help??


in reply to How do I quickly strip blank space from the beginning/end of a string?

Here's another attempt, with code that actually solves the problem of stripping of trailing whitespace.
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"; prin +t "$x|\n" if $verify; }, plus => sub { my $x = $str; $x =~ s/\s+$//; $method = "plus"; prin +t "$x|\n" if $verify; }, rx_rx => sub { my $x = $str; $x =~ /.*\S/g and $x =~ s/\G.*//; $met +hod = "rxrx"; print "$x|\n" if $verify; }, sexeger => sub { my $x = $str; ($x = reverse $x) =~ s/^\s+//; $x = re +verse $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 $v +erify; }, 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_r +x 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 d +etectsubstr 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% --
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.

Replies are listed 'Best First'.
Re^2: How do I quickly strip blank space from the beginning/end of a string?
by ig (Vicar) on Jul 22, 2009 at 11:53 UTC

    If most of your strings do not have trailing white space, then optimizing this case will be best. A little extra time to handle the few strings with trailing white space will not affect the average much. Do you know what percentage have trailing white space?

    String comparison is faster than a RE for detecting a space character. Is your trailing white space strictly spaces or might there be tabs, newlines or other white space characters to be removed?

    I played with a few alternatives.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://782134]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-03-19 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found