I think the alternation in the s/^\s+|\s+$// version causes significant time costs in large applications. I often work with tab-delimited files containing hundreds of thousands of lines. If I'm tab-splitting these lines and then trimming each one, I'm going to pick the double-regex approach each time.
I wrote some quick code that benchmarked the double-regex vs single-regex approach against three strings.
use Benchmark;
my @words = ('trim_unneeded',' front trim only','rear trim only ','
+ both side trim ');
for my $word (@words){
print "Benchmarking $word...\n\n";
timethese(1_000_000, {double => sub{ $word =~ s/^\s+//; $word =~ s
+/\s+$//; },
single => sub{ $word =~ s/^\s+|\s+$//; }})
}
The code was run on a Celeron D 2.8 GHz machine running XP with the following results:
'trim_unneeded'
Single Regex: 0.45 seconds
Double Regex: 2.27 seconds
' front trim only'
Single Regex: 0.67 seconds
Double Regex: 2.66 seconds
'rear trim only '
Single Regex: 0.67 seconds
Double Regex: 2.45 seconds
' both side trim '
Single Regex: 0.66 seconds
Double Regex: 2.44 seconds
That's after only 1,000,000 trims. In a 800,000 line file with 50 columns per line, we're talking about 40,000,000 trims. Assuming a linear scale, that means I give up about a minute of processing time per file per run. That's far less than the time it would have taken me to type two regexes.
Admittedly, it's a small optimization, and only valid for those who are processing files on the scale that I do, but for most people who end up typing the 'trim regex' often enough to complain about it on perlmonks, it probably applies.
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.