Perl optimisation help

Lennart Sorensen lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org
Fri Jun 9 14:33:26 UTC 2006


On Fri, Jun 09, 2006 at 12:47:14AM +0300, Peter wrote:
> %hash=();
> $idx=0;
> 
> cond loop {
>   $hash{$idx++}=$line;
> }

Isn't using a hash with numerical keys rather like using an array except
possibly less efficient?

> $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.
> 
> join is extremely slow. I will try it again in a different config. Using 
> join is a little like using '.' I think.

Well something like this might work faster than hashes (not sure which
are faster):

@array=();

cond loop {
	push @array,$line;
}

$res='';
foreach $line(@array) {
	$res=$res.$line;
}

Of course I fail to see why that would be faster to perl than just doing
the line concatenation in the first place.

Doing loops that make longer strings might be more efficient to cut down
on copying the string to many times.  ie:

$res='';
$cound=0;
# Do 10 at a time
while($count+10 < scalar(@array)) {
	$res=$res.$array[$count+0].$array[$count+1].$array[$count+2].$array[$count+3].$array[$count+4].$array[$count+5].$array[$count+6].$array[$count+7].$array[$count+8].$array[$count+9];
	$count=$count+10;
}
# Do whatever is left under 10 at the end
while($count < scalar(@array)) {
        $res=$res.$array[$count];
        $count=$count+1;
}
Could expand on that to do 100 at a time, then 10 at a time, then 1,
etc.

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