in reply to array pre-allocation trick
Your code on the first call:
On future calls it just creates @array and starts using it. So your tests should not show any advantage.
Move the "my @array;" outside of the function so that it survives from call to call:
Update follows{ my $initialized; my @array; sub example { unless ( $initialized) { $#array = $size_needed; $initialized++; } # fill the array } }
As soon as I submitted the above I realize that may not be what you want. You may be making this way harder than you need to.
If you need a new @array on each call. Just preallocate it each time:
sub example { my @array; $#array = $size_needed; # fill array -- this is where you may save time }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: array pre-allocation trick
by strat (Canon) on Oct 31, 2002 at 11:08 UTC |