http://www.perlmonks.org?node_id=782077

blahblahblah has asked for the wisdom of the Perl Monks concerning the following question:

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.