Next | Making Programs Faster | 103 |
sub preallocate { my (@c); $#c = 99999; for (my $i; $i < 100000; $i++) { $c[$i] = rand; } }
The answer was eventually provided by Chip Salzenberg
Perl has a clever optimization in it
Perl figures that @c got big once, so it is likely to get big again
When preallocate returns, @c is not deallocated
The next call re-uses the same space as the last call
And that leaves $#c = 99999 with nothing to do
In fact, it's a small waste of time because it's superfluous
So you pay the cost for your 'optimization'
But the gross benefit is zero because you already had the benefit
One optimization plus one optimization looks like zero optimizations
Next | Copyright © 2003 M. J. Dominus |