in reply to Re^6: Producing a list of offsets efficiently
in thread Producing a list of offsets efficiently
The point about amortized constant is critical. The convergence to that behaviour is quite fast. For instance if you break the 400,000 array into building 400 arrays of 1000 elements that you then push together, you'll find the same amortized constant coming into play both in building 1000 element arrays and in building the 400,000 element array. So instead of eliminating that overhead, you're now paying twice the overhead! (By separating arrays you've kept Perl from deciding to allocate big enough chunks.)
If you want to reduce that overhead, you can change the constant fairly easily by pre-extending the array at specified intervals. Assign by index and keep track of the size of the array. Any time you're about to pass the maximum size of the array, assign undef to a spot that is twice as far. That'll change the constant from 5 to 2, and the average reallocations from ranging from 5-6 down to ranging from 2-3. (At the cost of a constant amount of logic per iteration. I don't know which constant will be bigger - this might be a loss.)
Overall I'm going to guess, though, that with 400,000 offsets your biggest source of overhead is that you have 400,000 Perl scalars. If performance is a problem it might save effort to have the data packed into a long string. You might even use C to build said string.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Producing a list of offsets efficiently
by BrowserUk (Patriarch) on May 30, 2005 at 07:37 UTC | |
by tilly (Archbishop) on May 30, 2005 at 07:53 UTC | |
by BrowserUk (Patriarch) on May 30, 2005 at 08:11 UTC |