Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

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

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

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



      Results (82 votes). Check out past polls.

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