I'm using perl 5.6.1, and I don't get those results. Here's the script I'm using. I was just curious about leading spaces by itself, so I threw that in, but the WHILE_RE version is definitely slower than either the SEXEGER or the SINGLE_RE version. And on the leading & trailing white space stripping, the LEADTRAIL is version is quicker than the LTSAVE version.
#!/usr/local/bin/perl -w use strict; use Benchmark; my $str = " a b c d "; timethese(-5, { LEADING=>\&leading, LEADTRAIL=>\&lead_trail, LTSAVE=>\&lt_save, SEXEGER=>\&sexeger, SINGLE_RE=>\&single_re, WHILE_RE=>\&while_sub, }); sub leading { local $_ = $str; s/^\s+//; $_; } sub lead_trail { local $_ = $str; s/^\s+|\s+$//g; $_; } sub lt_save { local $_ = $str; s/^\s*(.*?)\s*$/$1/; $_; } sub sexeger { local $_ = reverse $str; s/^\s+//; reverse $str; } sub single_re { local $_ = $str; s/\s+$//; $_; } sub while_sub { local $_ = $str; 1 while s/\s$//; $_; } ~/tst >./tst3 Benchmark: running LEADING, LEADTRAIL, LTSAVE, SEXEGER, SINGLE_RE, WHI +LE_RE, eac h for at least 5 CPU seconds... LEADING: 5 wallclock secs ( 5.00 usr + 0.00 sys = 5.00 CPU) @ 77 +596.80/s ( n=387984) LEADTRAIL: 5 wallclock secs ( 5.22 usr + 0.00 sys = 5.22 CPU) @ 26 +504.21/s ( n=138352) LTSAVE: 5 wallclock secs ( 5.18 usr + 0.00 sys = 5.18 CPU) @ 18 +690.54/s ( n=96817) SEXEGER: 4 wallclock secs ( 5.35 usr + 0.00 sys = 5.35 CPU) @ 57 +972.52/s ( n=310153) SINGLE_RE: 5 wallclock secs ( 5.23 usr + 0.00 sys = 5.23 CPU) @ 56 +434.42/s ( n=295152) WHILE_RE: 6 wallclock secs ( 5.00 usr + 0.00 sys = 5.00 CPU) @ 36 +090.00/s ( n=180450)
Update: I cut 'n pasted the code and output, then fixed LTSAVE, then forgot to repaste. I guess no one looked closely enough to notice that LT_SAVE was actually beating LEADTRAIL. Its all fixed now though :)

In reply to Re: japhy blabs about regexes (again) by runrig
in thread japhy blabs about regexes (again) by japhy

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, 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:
    & &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.