Nice, never occurred to me to use join split for that task.

Though using s/// seems to be noticeable faster, at least if not used with an alternating regex:

use strict; use warnings; use Benchmark qw(cmpthese); my $short = q( a b c ); my $long = join('x', map {' ' x (int(rand(20))+1)} 1 .. 100); my %func = ( triple_s => <<'EOFUNC', sub { my $str = $_; $str =~ s/^\s*//; $str =~ s/\s*$//; $str =~ s/\s+/ /g; $str; } EOFUNC double_s => <<'EOFUNC', sub { my $str = $_; $str =~ s/^\s*|\s*$//; $str =~ s/\s+/ /g; $str; } EOFUNC join_split => <<'EOFUNC', sub { my $str = $_; join ' ', split ' ', $str; } EOFUNC ); sub make { my ($str, $f) = @_; ($f = $func{$f}) =~ s/_/$str/; eval $f; } print "\nShort string:\n"; cmpthese(-1, { 'triple-s' => make(qw(short triple_s)), 'double-s' => make(qw(short double_s)), 'join-split' => make(qw(short join_split)), }); print "\nLong string:\n"; cmpthese(-1, { 'triple-s' => make(qw(long triple_s)), 'double-s' => make(qw(long double_s)), 'join-split' => make(qw(long join_split)), });

giving:

Short string: Rate triple-s join-split double-s triple-s 305867/s -- -31% -37% join-split 441107/s 44% -- -9% double-s 482473/s 58% 9% -- Long string: Rate triple-s join-split double-s triple-s 4775/s -- -69% -86% join-split 15251/s 219% -- -55% double-s 33822/s 608% 122% --

Update, in reaction to ikegami's reply:

My above code is seriously flawed. Not only the unprovoked change from + to *, but moreso in forgetting the mandatory g modifier in the regex with alternation which should trim the start and the end of the argument string!

Should have told me something, that the version with the worse reputation scored best!

So here comes my corrected and expanded try:

use strict; use warnings; use Benchmark qw(cmpthese); my $short = q( a b c ); my $long = join('x', map {' ' x (int(rand(20))+1)} 1 .. 100); my %func = ( triple_s => <<'EOFUNC', sub { my $str = $_; $str =~ s/^\s*//; $str =~ s/\s*$//; $str =~ s/\s+/ /g; $str; } EOFUNC triple_sp => <<'EOFUNC', sub { my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str =~ s/\s+/ /g; $str; } EOFUNC triple_sp_r => <<'EOFUNC', sub { my $str = $_; $str =~ s/\s+/ /g; $str =~ s/^\s+//; $str =~ s/\s+$//; $str; } EOFUNC double_s => <<'EOFUNC', sub { my $str = $_; $str =~ s/^\s*|\s*$//g; $str =~ s/\s+/ /g; $str; } EOFUNC double_sp => <<'EOFUNC', sub { my $str = $_; $str =~ s/^\s+|\s+$//g; $str =~ s/\s+/ /g; $str; } EOFUNC double_sp_r => <<'EOFUNC', sub { my $str = $_; $str =~ s/\s+/ /g; $str =~ s/^\s+|\s+$//g; $str; } EOFUNC join_split => <<'EOFUNC', sub { my $str = $_; join ' ', split ' ', $str; } EOFUNC ); sub make { my ($str, $f) = @_; ($f = $func{$f}) =~ s/_/$str/; eval $f; } print "\nShort string:\n"; cmpthese(-5, { '3-s' => make(qw(short triple_s)), '2-s' => make(qw(short double_s)), '3-sp' => make(qw(short triple_sp)), '2-sp' => make(qw(short double_sp)), '3-sp_r' => make(qw(short triple_sp_r)), '2-sp_r' => make(qw(short double_sp_r)), 'j-split' => make(qw(short join_split)), }); print "\nLong string:\n"; cmpthese(-5, { '3-s' => make(qw(long triple_s)), '2-s' => make(qw(long double_s)), '3-sp' => make(qw(long triple_sp)), '2-sp' => make(qw(long double_sp)), '3-sp_r' => make(qw(long triple_sp_r)), '2-sp_r' => make(qw(long double_sp_r)), 'j-split' => make(qw(long join_split)), });

Which gives on this machine:

Short string: Rate 2-s 2-sp 2-sp_r 3-s 3-sp_r 3-sp j-spl +it 2-s 114468/s -- -14% -26% -28% -42% -47% -4 +7% 2-sp 132675/s 16% -- -14% -17% -33% -38% -3 +8% 2-sp_r 155014/s 35% 17% -- -3% -21% -28% -2 +8% 3-s 159352/s 39% 20% 3% -- -19% -26% -2 +6% 3-sp_r 196609/s 72% 48% 27% 23% -- -8% - +9% 3-sp 214344/s 87% 62% 38% 35% 9% -- - +0% j-split 214880/s 88% 62% 39% 35% 9% 0% +-- Long string: Rate 2-s 2-sp 3-s j-split 2-sp_r 3-sp 3-sp_ +r 2-s 1961/s -- -3% -37% -76% -77% -83% -85 +% 2-sp 2029/s 3% -- -34% -76% -76% -83% -85 +% 3-s 3092/s 58% 52% -- -63% -63% -74% -77 +% j-split 8343/s 325% 311% 170% -- -1% -29% -38 +% 2-sp_r 8443/s 331% 316% 173% 1% -- -28% -38 +% 3-sp 11694/s 496% 476% 278% 40% 39% -- -13 +% 3-sp_r 13512/s 589% 566% 337% 62% 60% 16% - +-

In reply to Re: removing the white spaces by pKai
in thread removing the white spaces by Anonymous Monk

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.