I wrote a small benchmark, under the assumption that the implementation is not allowed to modify the original string, and if it does it has to take a copy first.

I found out that all versions are extremely fast, and thus not easy to benchmark. So probably my benchmark is flawed, take it with a grain of salt.

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 m_substr { my $s = $x; for (my $i=16*int(length($s)/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), unpack => wrap('unpack',\&up), substr => wrap('substr',\&m_substr), readl => wrap('readline', \&readline), })

Here are the results (with perl5.10.0):

Rate readl subst unpack substr readl 78783885/s -- -17% -18% -24% subst 95302528/s 21% -- -1% -8% unpack 96105165/s 22% 1% -- -8% substr 103929656/s 32% 9% 8% --
And then I ran it again:
Rate subst readl substr unpack subst 81695687/s -- -1% -36% -65% readl 82899185/s 1% -- -35% -65% substr 128382089/s 57% 55% -- -46% unpack 235800076/s 189% 184% 84% --
and thought "WTF?"

I increased the length of the source string by a factor of 100, and started the benchmark again. It still runs (after about 15 minutes).

My conclusions are that the differences are so small (or my benchmark so flawed...) that you can basically neglect them.


In reply to Re^2: How to split a string into chunks of length n or less by moritz
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.