[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Speed question



On Wed, May 31, 2000 at 09:49:05PM +0200, Giorgio Valoti wrote:

> *but*: until the 'for' is below 4000 iterations, the script finish in 2
> seconds, more or less; if I repeat the loop 5000 times I have to wait 30
> secs!
> Am I missing something ( as usual)?

I'm guessing that the problem is with this part of the code:

> foreach my $key (keys %database ) {
>   print DUMP "$key\n". ("=" x (length($key)+1))."\n";
>   print DUMP "$database{$key}\n\n";
> }
> 

The foreach loop evaluates its control expression in a list context,
so the keys function will create a (potentially large) temporary list
of all the keys in memory and iterate over the temporary list.

You might be better of grabbing on key at a time by using the iterator
built into the scalar context version of keys().

while(defined(my $key = keys %database) ) {
  print DUMP "$key\n". ("=" x (length($key)+1))."\n";
  print DUMP "$database{$key}\n\n";
}

-- 
Andrew Langmead

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org