Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-28 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found