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 |