Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
We have a subroutine that we call tens of thousands of times per page in our old, large codebase. It can't fully trust it's input (see last 3 words of previous sentence), so it always trims the whitespace from its arguments. It uses the method suggested in perlfaq4:
How do I strip blank space from the beginning/end of a string? (contributed by brian d foy) A substitution can do this for you. For a single line, you want to replace all the leading or trailing whitespace with nothing. You can do that with a pair of substitutions. s/^\s+//; s/\s+$//;
Recently I've been alerted to the fact that there are possibly much faster ways to do this. It seems japhy has written much about this, including a good overview with benchmarks at Regexes are slow (or, why I advocate String::Index). That post is pretty old, so I tried some of his benchmarks on perl 5.10. I found similar results. However, I noticed that his benchmarks all have long data, but my situation is that the data length will be varied, and often will be very short. Here are my results, once with long data and once short:

Update: As Ikegami pointed out, my benchmark had little to do with my stated question. See reply below for a new attempt at this.

use Benchmark 'cmpthese'; my $str = "alphabet X alphabet" x 100 . "junk at the end" x 10; cmpthese(-5, { last => sub { my $x = $str; $x =~ s/([A-Z])[^A-Z]*$/$1/ }, capt_repl => sub { my $x = $str; $x =~ s/(.*[A-Z]).*/$1/ }, rx_rx => sub { my $x = $str; $x =~ /.*[A-Z]/g and $x =~ s/\G.*// }, sexeger => sub { my $x = $str; ($x = reverse $x) =~ s/^[^A-Z]+//; $x = reverse $x; }, }); print "-------------- now a short one -------------------------------- +\n"; $str = "a very short one"; cmpthese(-5, { last => sub { my $x = $str; $x =~ s/([A-Z])[^A-Z]*$/$1/ }, capt_repl => sub { my $x = $str; $x =~ s/(.*[A-Z]).*/$1/ }, rx_rx => sub { my $x = $str; $x =~ /.*[A-Z]/g and $x =~ s/\G.*// }, sexeger => sub { my $x = $str; ($x = reverse $x) =~ s/^[^A-Z]+//; $x = reverse $x; }, }); __END__ [16:10] <Joe> Rate last sexeger capt_repl rx_r +x [16:10] <Joe> last 4329/s -- -78% -82% -86 +% [16:10] <Joe> sexeger 19386/s 348% -- -19% -37 +% [16:10] <Joe> capt_repl 23859/s 451% 23% -- -22 +% [16:10] <Joe> rx_rx 30778/s 611% 59% 29% - +- [16:10] <Joe> -------------- now a short one ------------------------- +------- [16:10] <Joe> Rate sexeger capt_repl rx_rx la +st [16:10] <Joe> sexeger 261969/s -- -4% -6% -7 +0% [16:10] <Joe> capt_repl 271691/s 4% -- -3% -6 +8% [16:10] <Joe> rx_rx 279271/s 7% 3% -- -6 +7% [16:10] <Joe> last 859023/s 228% 216% 208% +--
You might call this micro-optimization, but my users really would notice a couple-second difference as these scripts are running in a web app. I'm wondering if I can zero in on a particular string length where the "last" method degrades and the "segexer" becomes better. I don't have a lot of benchmarking experience, so even if I find that spot I'm not sure I'll trust my data or my methods. Just looking for advice from those who may have pondered this before or have insight that they can lend.

Thanks,
Joe

Update:crossed out non-applicable benchmark. See reply below for my latest attempt.


In reply to 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 cooling their heels in the Monastery: (7)
    As of 2019-06-27 08:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Is there a future for codeless software?



      Results (111 votes). Check out past polls.

      Notices?