Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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 ( #782267=note: print w/replies, xml ) Need Help??


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

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.

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*$//; }, plus => sub { my $x = $str; $x =~ s/\s+$//; }, rx_rx => sub { my $x = $str; $x =~ /.*\S/g and $x =~ s/\G.*/ +/; }, sexeger => sub { my $x = $str; ($x = reverse $x) =~ s/^\s+//; +$x = reverse $x; }, lookbehind => sub { my $x = $str; $x =~ s/.\K\s+$//; }, detectregex => sub { my $x = $str; if ($x =~ /\s/){($x = reverse $ +x) =~ s/^\s+//; $x = reverse $x;} }, detectsubstr => sub { my $x = $str; if (substr($x, -1) =~ /^\s/){($ +x = reverse $x) =~ s/^\s+//; $x = reverse $x;} }, capture => sub { my $x = $str; $x =~ m/(.*\S)/; $x = $1; }, condcapture => sub { my $x = $str; if (substr($x, -1) =~ /^\s/) { +$x =~ s/(.*\S).*/$1/; } }, condcapture2 => sub { my $x = $str; if ((reverse $x) =~ /^\s/) { +$x =~ s/(.*\S).*/$1/; } }, condcapture3 => sub { my $x = $str; if (substr($x, -1) eq ' ') { +$x =~ s/(.*\S).*/$1/; } }, chop => sub { my $x = $str; my $y; while(($y = chop($x)) eq + ' ') {} $x .= $y; }, chop2 => sub { my $x = $str; while(substr($x, -1) eq ' ') { +chop($x) } }, chop3 => sub { my $x = $str; if (substr($x, -1) eq ' ') { ch +op($x) while(substr($x, -1) eq ' '); } }, walk => sub { my $x = $str; if (substr($x, -1) eq ' ') { my + $i = 0; while(substr($x, --$i, 1) eq ' ') {} $x = substr($x, 0, ++$i +) } }, }); __END__ $ ./test.pl short Rate lookbehind last detectregex rx_rx capture plus + sexeger chop detectsubstr condcapture2 chop2 condcapture chop3 condc +apture3 walk lookbehind 166771/s -- -6% -64% -68% -72% -74% + -76% -82% -86% -86% -87% -87% -91% + -92% -92% last 177783/s 7% -- -62% -65% -70% -72% + -74% -80% -85% -85% -86% -87% -91% + -91% -91% detectregex 468229/s 181% 163% -- -9% -22% -26% + -33% -48% -61% -61% -64% -65% -76% + -76% -77% rx_rx 513883/s 208% 189% 10% -- -14% -19% + -26% -43% -57% -57% -61% -61% -74% + -74% -74% capture 599801/s 260% 237% 28% 17% -- -6% + -14% -34% -50% -50% -54% -55% -69% + -70% -70% plus 637029/s 282% 258% 36% 24% 6% -- + -8% -29% -47% -47% -51% -52% -67% + -68% -68% sexeger 694745/s 317% 291% 48% 35% 16% 9% + -- -23% -42% -42% -47% -48% -64% + -65% -65% chop 903167/s 442% 408% 93% 76% 51% 42% + 30% -- -24% -24% -31% -32% -54% + -55% -55% detectsubstr 1192708/s 615% 571% 155% 132% 99% 87% + 72% 32% -- -0% -9% -10% -39% + -40% -40% condcapture2 1195367/s 617% 572% 155% 133% 99% 88% + 72% 32% 0% -- -9% -10% -38% + -40% -40% chop2 1308306/s 684% 636% 179% 155% 118% 105% + 88% 45% 10% 9% -- -1% -33% + -34% -35% condcapture 1323455/s 694% 644% 183% 158% 121% 108% + 90% 47% 11% 11% 1% -- -32% + -34% -34% chop3 1943341/s 1065% 993% 315% 278% 224% 205% + 180% 115% 63% 63% 49% 47% -- + -2% -3% condcapture3 1992099/s 1095% 1021% 325% 288% 232% 213% + 187% 121% 67% 67% 52% 51% 3% + -- -0% walk 2001064/s 1100% 1026% 327% 289% 234% 214% + 188% 122% 68% 67% 53% 51% 3% + 0% -- $ ./test.pl long Rate lookbehind last plus detectregex capture sexe +ger rx_rx condcapture2 chop detectsubstr condcapture chop2 walk con +dcapture3 chop3 lookbehind 1477/s -- -12% -87% -98% -99% - +99% -99% -99% -100% -100% -100% -100% -100% + -100% -100% last 1678/s 14% -- -85% -98% -98% - +98% -99% -99% -100% -100% -100% -100% -100% + -100% -100% plus 11508/s 679% 586% -- -88% -88% - +89% -94% -96% -98% -98% -98% -98% -99% + -99% -99% detectregex 96098/s 6405% 5626% 735% -- -4% +-7% -46% -65% -84% -87% -87% -87% -90% + -90% -90% capture 99640/s 6645% 5837% 766% 4% -- +-4% -44% -64% -83% -86% -86% -87% -89% + -89% -90% sexeger 103603/s 6913% 6073% 800% 8% 4% + -- -42% -62% -82% -86% -86% -86% -89% + -89% -89% rx_rx 178739/s 11999% 10550% 1453% 86% 79% +73% -- -35% -70% -75% -75% -76% -81% + -81% -82% condcapture2 274909/s 18509% 16280% 2289% 186% 176% 1 +65% 54% -- -53% -62% -62% -64% -71% + -71% -72% chop 588574/s 39741% 34970% 5015% 512% 491% 4 +68% 229% 114% -- -18% -19% -22% -38% + -38% -39% detectsubstr 717510/s 48469% 42652% 6135% 647% 620% 5 +93% 301% 161% 22% -- -2% -5% -24% + -24% -26% condcapture 729020/s 49248% 43338% 6235% 659% 632% 6 +04% 308% 165% 24% 2% -- -4% -23% + -23% -25% chop2 758962/s 51275% 45122% 6495% 690% 662% 6 +33% 325% 176% 29% 6% 4% -- -20% + -20% -22% walk 945612/s 63909% 56244% 8117% 884% 849% 8 +13% 429% 244% 61% 32% 30% 25% -- + -0% -3% condcapture3 948315/s 64092% 56405% 8141% 887% 852% 8 +15% 431% 245% 61% 32% 30% 25% 0% + -- -2% chop3 971103/s 65635% 57763% 8339% 911% 875% 8 +37% 443% 253% 65% 35% 33% 28% 3% + 2% -- $ ./test.pl shortspaces Rate lookbehind last condcapture condcapture2 condcap +ture3 plus walk detectsubstr chop chop3 detectregex chop2 rx_rx captu +re sexeger lookbehind 62741/s -- -21% -57% -67% + -69% -75% -80% -82% -84% -84% -84% -85% -87% -8 +8% -88% last 79377/s 27% -- -45% -58% + -61% -68% -74% -78% -79% -80% -80% -81% -83% -8 +4% -85% condcapture 144738/s 131% 82% -- -23% + -28% -41% -53% -59% -62% -63% -64% -65% -69% -7 +2% -73% condcapture2 188671/s 201% 138% 30% -- + -7% -23% -39% -47% -51% -52% -53% -54% -60% -6 +3% -65% condcapture3 202323/s 222% 155% 40% 7% + -- -18% -34% -43% -47% -48% -49% -50% -57% -6 +0% -63% plus 246071/s 292% 210% 70% 30% + 22% -- -20% -30% -36% -37% -38% -40% -47% -5 +2% -55% walk 307897/s 391% 288% 113% 63% + 52% 25% -- -13% -20% -21% -23% -25% -34% -4 +0% -43% detectsubstr 353906/s 464% 346% 145% 88% + 75% 44% 15% -- -8% -9% -11% -13% -24% -3 +1% -35% chop 384362/s 513% 384% 166% 104% + 90% 56% 25% 9% -- -2% -3% -6% -18% -2 +5% -29% chop3 390483/s 522% 392% 170% 107% + 93% 59% 27% 10% 2% -- -2% -4% -17% -2 +4% -28% detectregex 398141/s 535% 402% 175% 111% + 97% 62% 29% 12% 4% 2% -- -3% -15% -2 +2% -27% chop2 408424/s 551% 415% 182% 116% + 102% 66% 33% 15% 6% 5% 3% -- -13% -2 +0% -25% rx_rx 468273/s 646% 490% 224% 148% + 131% 90% 52% 32% 22% 20% 18% 15% -- - +8% -14% capture 510839/s 714% 544% 253% 171% + 152% 108% 66% 44% 33% 31% 28% 25% 9% +-- -6% sexeger 544514/s 768% 586% 276% 189% + 169% 121% 77% 54% 42% 39% 37% 33% 16% +7% --
  • Comment on Re^2: How do I quickly strip blank space from the beginning/end of a string?
  • Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2019-06-17 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (79 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!