| Next | Making Programs Faster | 100 |
Consider this:
while (<>) {
my ($n, $text) = split /: /, $_, 2;
$line[$n] = $text;
}
Each time $n is larger than @line, the array is extended
It might have to be copied to a new, larger region of memory
Why not extend all at once?
If you know that $n will get as large as 1000000, then:
$#line = 1000000;
while (<>) {
my ($n, $text) = split /: /, $_, 2;
$line[$n] = $text;
}
This should save time
| Next | ![]() |
Copyright © 2003 M. J. Dominus |