Perl optimisation help

Lennart Sorensen lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org
Thu Jun 8 19:59:13 UTC 2006


On Thu, Jun 08, 2006 at 10:37:14PM +0300, Peter wrote:
> Wow, that was fast ;-) Your answer got here before the echo of my own 
> message. Thanks, that was useful.
> 
> But more exactly, does anyone know a more efficient way to increase a 
> string variable's length, written like:
> 
>   $var = $var.$more
> 
> where this needs to be done 100,000 times in realtime (say, a few 
> seconds) ?
> 
> $more is usually a line of text (~80chars) and $var can grow to 8 Megs.
> 
> I have thought of using a hash indexed by line numbers instead of a 
> simple var. This will almost certainly be faster (depending on how 
> horribly (?) the '.' operator is implemented). Before I do this, has 
> anyone got a pointer to a discussion document on such issues ?
> 
> '.' seems to run in O(n^2) so far. Maybe Perl uses the C string storage 
> convention internally instead of Pascal ? (ouch)

Quite likely given how easy it is to make perl use c coded modules.

How would this perform:
@myvars = ();
loop over input {
	push @myvars,$more;
}
$var = join('', at myvars);
@myvars = (); # Just in case that helps perl free the memory sooner

Does that run any faster or is join implemented badly too?

Len Sorensen
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list