On Thu, 18 May 2000, brian moseley wrote:

> On Thu, 18 May 2000, Autarch wrote:
> 
> > C seems like serious overkill for something to simply
> > generate plain text output.  How slow is making a string
> > in perl compared to doing it in C?  I can't imagine
> > there's to much of a difference.
> 
> pretty slow if you build a string using .= instead of using
> smarter methods, like pushing strings onto an array and then
> joining it.

You tried to sell me that when I was at CP, and I didn't buy it then
either.  This is a benchmark of .= versus join.  500000 20-byte strings
are joined:

    Concat:  2 wallclock secs ( 1.34 usr +  0.27 sys =  1.61 CPU)
      Join:  4 wallclock secs ( 3.63 usr +  0.19 sys =  3.82 CPU)

.= concatenation is way faster.  Also, building a 500000-element array in
Perl takes up vastly more memory than building a 10000000-byte string.  
The string and the Perl process together require an RSS of 11MB, while the
array and the Perl process eat an RSS of 51MB.

Here is the benchmark program used:


my $iter = shift;

sub concat {
        my $this;

        for (my $i = 0; $i < $iter; $i++) {
                $this .= 'A'x20;
        }
}

sub arrayjoin {
        my @this;

        for (my $i = 0; $i < $iter; $i++) {
                push(@this, 'A'x20);
        }

        join('', @this);
}

timethese(1, { 'Concat' => \&concat, 'Join' => \&arrayjoin });


The system was an Intel Pentium III 500 MHz with 128 MB of RAM and Linux
2.2.15.  Swap was turned off.

-jwb

Reply via email to