in reply to Memory efficiency with arrays

You are correct that a pre-extended array is marginally more efficient to push stuff onto than one started from scratch. But the difference is generally small in comparison with the effort it takes to create the stuff in the array.

With current versions of Perl, growing an array with push is fast. Walking through it with shift and pop is fast. (I would suspect that pop is marginally faster because it does less accounting.) With current versions of Perl it is slow to unshift. But this will change.

In short, unless you are encountering problems, don't worry about it.

(That notwithstanding, if you have to only go through it once think about whether there is any way to redesign your program to operate incrementally so you never have to have it all in memory at once.)

  • Comment on Re (tilly) 1: Memory efficiency with arrays

Replies are listed 'Best First'.
Re: Memory efficiency with arrays
by Dominus (Parson) on Jan 05, 2001 at 07:39 UTC
    Says tilly:
    With current versions of Perl it is slow to unshift.

    It's probably also worth pointing out that unshift is fast if the array has previously been shifted. The shift leaves a gap at the beginning of the memory block, and when you unshift, Perl sticks the new data into the gap.