in reply to Re: Various ways to concatenating an array of strings
in thread Various ways to concatenating an array of strings
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) :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Various ways to concatenating an array of strings
by Tanktalus (Canon) on Mar 30, 2006 at 20:18 UTC | |
|
Re^3: Various ways to concatenating an array of strings
by ikegami (Patriarch) on Mar 30, 2006 at 19:59 UTC | |
by hawtin (Prior) on Mar 30, 2006 at 20:03 UTC |