At 16:32 05/05/2000 +1000, Ian Boreham wrote:
>At 05:00 PM 5/4/00 -0700, "Jeff Clites" <[EMAIL PROTECTED]> wrote:
>>It's just an assumption on my part (I've never actually timed it), but when
>>you add to a string I believe that Perl has to realloc(),
>
>I believe that it normally allocates a bit of extra storage to allow some
>growth without reallocation, and reallocates larger and larger amounts in
>anticipation every time you extend the string to something greater than its
>previous limit. This would mean that you would not reallocate every time -
>only sometimes.

I've tried a few benchmarks, and it appears to depend a lot on the size of
the string one is appending/pushing.

for $str .= 'a' and push @str, 'a' I get something like this:

Benchmark: timing 1000000 iterations of catenation, pushing...
append:  4 wallclock secs ( 3.34 usr +  0.00 sys =  3.34 CPU)
   push:  6 wallclock secs ( 5.66 usr +  0.00 sys =  5.66 CPU)
 
where it's faster to append. However, replacing 'a' with 'a' x 1000 I get
the following:

Benchmark: timing 5000 iterations of catenation, pushing...
append: 10 wallclock secs ( 9.83 usr +  0.00 sys =  9.83 CPU)
   push:  0 wallclock secs ( 0.17 usr +  0.00 sys =  0.17 CPU)
            (warning: too few iterations for a reliable count)

For a medium-sized string (100 chars) which is the kind of string I might
expect to get if I'm reading one line at a time from a filehandle the
result is pretty much the same:

Benchmark: timing 50000 iterations of catenation, pushing...
append: 10 wallclock secs (10.21 usr +  0.00 sys = 10.21 CPU)
   push:  1 wallclock secs ( 0.49 usr +  0.00 sys =  0.49 CPU)

So unless one is reading one char at a time, it looks like using an array
is a sure bet. Of course you still have to join afterwards, but it only
happens once.



.Robin
Those of you who think you know everything are very annoying to those of us
who do.


---
You are currently subscribed to perl-xml as: [[email protected]]
To unsubscribe, forward this message to
         [EMAIL PROTECTED]
For non-automated Mailing List support, send email to  
         [EMAIL PROTECTED]

Reply via email to