You missed the corrections some have added. There is still the extra space at the end in certain cases. See oshalla's post and my update above.

I added my second code snippet and ran this:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my $x = "0123456789abcdefg" x 6000 . "XXX"; sub subst { my $s = $x; $s =~ s/(.{16})/$1 /sg; $s }; sub up { join ' ', unpack '(A16)*', $x }; sub re { join ' ', $x=~ /(.{1,16})/g }; sub m_substr { my $s = $x; for (my $i=16*int((length($s)-1)/16);$i;$i-=16) { substr($s,$i,0)=" "; } $s; } sub readline { local $/ = \16; my $s; open my $handle, '<', \$x; $s = <$handle>; while (<$handle>) { $s .= ' ' . $_; } $s; } my $ref = subst(); sub wrap { my ($impl, $sub) = @_; die "Wrong result for $impl!" unless $sub->() eq $ref; } cmpthese(-1, { subst => wrap('subst', \&subst), re => wrap('re', \&re), unpack => wrap('unpack',\&up), substr => wrap('substr',\&m_substr), readl => wrap('readline', \&readline), })
My results:
Rate unpack substr subst re readl unpack 29656695/s -- -5% -8% -14% -16% substr 31367657/s 6% -- -3% -9% -11% subst 32410531/s 9% 3% -- -6% -8% re 34622792/s 17% 10% 7% -- -2% readl 35251272/s 19% 12% 9% 2% --

s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

In reply to Re^3: How to split a string into chunks of length n or less by Skeeve
in thread How to split a string into chunks of length n or less by isync

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.