Perl optimisation help

Rick Delaney rick-h4KjNK7Mzas at public.gmane.org
Fri Jun 9 00:42:26 UTC 2006


On Fri, Jun 09, 2006 at 12:47:14AM +0300, Peter wrote:
> 
> On Thu, 8 Jun 2006, Lennart Sorensen wrote:
> 
> >On Thu, Jun 08, 2006 at 11:07:45PM +0300, Peter wrote:
> >>I did it with a hash and it is 5 times faster. Still I would like to
> >>know what the fastest way to do $var=$var.$add; is.
> >
> >Perl don't have someting icky like $var .= $more; does it?
> 
> Not that I know of it.

It does.  If you don't like the perlmonks suggestion you might want to
try the local Toronto Perl Mongers mailing list, tpm-BnhL/mGhxvQEbZ0PF+XxCw at public.gmane.org

> >How do you do it with a hash?
> 
> %hash=();
> $idx=0;
> 
> cond loop {
>   $hash{$idx++}=$line;
> }
> 
> $res='';
> $i=0;
> while($i<$idx) {
>   $res = $res.$hash{$i++};
> }
> 
> this is fast, and could be faster by adding buckets. I think that Perl 
> measures the length of the string and caches it. If the next access is 
> 'soon enough' the length must not be calculated again. The speedup 
> factor is between 6 and 13 times (!) vs. using '.' standalone.

Your problem is certainly not with '.' since you are still using it.
More likely you have nested loops or something but it is really
difficult to tell what you're trying to do with your pseudocode.

If all you want is all input in one variable then there are many ways to
do so directly.  One would be

    $var = join "", <INPUT>;

Faster would be

    { local $/; $var = <INPUT>; }


-- 
Rick Delaney
rick-h4KjNK7Mzas at public.gmane.org
--
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