As I said in my original post I rejected join() for some good reason months ago. All your reply says is that join is the most obvious solution (which I agree it is), but it didn't work in my case.

Thank you for your input but your test of course proves nothing, my issue was with thousands of differently sized strings not 8 strings the same size. If I rewrite your test as:

use strict; use warnings; use Benchmark qw(cmpthese); my @strings; for(my $i=0;$i<1000;$i++) { $strings[$i] = (sprintf("%06d",$i)) x rand(1000); } my $asign1; my $asign2; cmpthese (-1, { pack => sub {$asign2 = pack("a*" x($#strings + 1),@strings)}, join => sub {$asign1 = join '', @strings}, } );

Then the results are:

Rate join pack join 108/s -- -21% pack 137/s 27% --

Having played with this I now recall that the efficiency problem was not with speed but with memory usage. Under some pathalogical cases the join was consuming vast quantities of memory (that was under a previous version of Perl and maybe I should study it again).

So thank you for helping me illustrate that pack is quicker than join (in some cases) :-)


In reply to Re^2: Various ways to concatenating an array of strings by hawtin
in thread Various ways to concatenating an array of strings by hawtin

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.