If you try the same benchmark with the + modifier instead of the * modifier you will see which is truely fastest:

use Benchmark q{cmpthese}; my @arr = ( q{ fdsgehw fw wwfe w } ) x 5000; cmpthese -5, { "alternation*" => sub { my @new = @arr; s{ ^\s* | \s*$ }{}gx for @new; }, "alternation+" => sub { my @new = @arr; s{ ^\s+ | \s+$ }{}gx for @new; }, capture => sub { my @new = @arr; s{ ^\s* (.*?) \s*$ }{$1}x for @new; }, splitJoin => sub { my @new = @arr; $_ = join q{ }, split for @new; }, "twoStage*" => sub { my @new = @arr; s{ ^\s* }{}x for @new; s{ \s*$ }{}x for @new; }, "twoStage+" => sub { my @new = @arr; s{ ^\s+ }{}x for @new; s{ \s+$ }{}x for @new; }, "twoStageComma*" => sub { my @new = @arr; s{ ^\s* }{}x, s{ \s*$ }{}x for @new; }, "twoStageComma+" => sub { my @new = @arr; s{ ^\s+ }{}x, s{ \s+$ }{}x for @new; }, };
Rate alternation* capture alternation+ twoStage* twoS +tageComma* splitJoin twoStage+ twoStageComma+ alternation* 29.8/s -- -1% -18% -34% + -35% -44% -65% -66% capture 30.2/s 1% -- -17% -33% + -34% -43% -64% -66% alternation+ 36.4/s 22% 20% -- -20% + -21% -31% -57% -59% twoStage* 45.3/s 52% 50% 24% -- + -1% -15% -46% -48% twoStageComma* 45.9/s 54% 52% 26% 1% + -- -13% -46% -48% splitJoin 53.1/s 78% 76% 46% 17% + 16% -- -37% -40% twoStage+ 84.6/s 184% 180% 132% 87% + 84% 59% -- -4% twoStageComma+ 87.8/s 194% 191% 141% 94% + 91% 65% 4% --

In reply to Re^3: Trimming whitespaces methods by jwkrahn
in thread Trimming whitespaces methods by harishnuti

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.