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% --
In reply to Re^2: Pre-grow a string (MTOW)
by almut
in thread Pre-grow a string
by diotalevi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |