Can you tell my why physi's code is slower than the rest? He suggested this:

$data =~ s/(^\s*|\s*$)//g;

I added these two subs to your benchmark:

sub both_at_once { my $data = $testdata; $data =~ s/(^\s+|\s+$)//g; return $data; } sub both_at_once2 { my $data = $testdata; $data =~ s/(^\s*|\s*$)//g; return $data; }
And this was the result:

Benchmark: timing 100000 iterations of both_at_once, both_at_once2, do +tstar, first_n_last_1, first_n_last_2... both_at_once: 10 wallclock secs ( 9.04 usr + 0.00 sys = 9.04 CPU) @ +11061.95/s (n=100000) both_at_once2: 11 wallclock secs (10.40 usr + 0.00 sys = 10.40 CPU) @ + 9615.38/s (n=100000) dotstar: 9 wallclock secs ( 8.30 usr + 0.00 sys = 8.30 CPU) @ 12 +048.19/s (n=100000) first_n_last_1: 6 wallclock secs ( 5.77 usr + 0.00 sys = 5.77 CPU) +@ 17331.02/s (n=100000) first_n_last_2: 2 wallclock secs ( 2.31 usr + 0.00 sys = 2.31 CPU) +@ 43290.04/s (n=100000)
Unless I'm mistaken, the pattern alternation (^\s+|\s+$) will try to match both patterns on every character. But, does the engine not know to disregard the ^\s+ except at the beginning of the string, and likewise for \s+$, only trying to match at the end? Just curious as to why this is so slow.

In reply to Re(2): Dot star okay, or not? by Cirollo
in thread Dot star okay, or not? by Cirollo

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.