in reply to Re: Pre-grow a string (MTOW)
in thread Pre-grow a string

my $string= ' ' x 2**19; $string= '';

I dimly remember having seen this being suggested somewhere else... However it doesn't actually seem to result in speeding up things (if that's the idea behind avoiding reallocations). On my machine and version of Perl it's in fact marginally slower.

use Benchmark "cmpthese"; sub pre { my $str = " " x 2**19; $str = ""; $str .= "aaaa" for 1..2**17; } sub std { my $str = ""; $str .= "aaaa" for 1..2**17; } cmpthese( -1, { 'pre' => 'pre()', 'std' => 'std()' });

Results:

Rate pre std pre 43.6/s -- -7% std 46.7/s 7% --

Replies are listed 'Best First'.
Re^3: Pre-grow a string (speed)
by tye (Sage) on Aug 02, 2007 at 17:45 UTC

    Yes, what is somewhat sucky about it is that it allocates the space twice, initializes one copy then copies it (then frees one); though I haven't checked this assumption of mine. Which means iterating over the requested size twice. Pre-sizing avoids reallocating. Reallocating is done exponentially (doubling the size each time) and so averages out to copying the maximum size about twice (unless you get lucky and reallocate in-place, rather unlikely in my experience). So it isn't surprising that the performance difference is minimal between those two choices.

    But note that pre-allocating will likely reduce a bit of memory fragmentation by avoiding leaving those power-of-two-sized buffers of now-free space in its wake.

    - tye