in reply to Re: Shift, Pop, Unshift and Push with Impunity!
in thread Shift, Pop, Unshift and Push with Impunity!

Speeding up unshift may explain what is going on.

Through Perl 5.6.1, unshift left the array aligned on the left. So if you build up an array with unshift's it is forced to realign it on every single unshift. If you build an array with repeated unshifts this makes the cost of each linear, for quadratic total time. What tilly's patch does is realign it indented to the length of the current array. So you only realign after you unshift the length of the array elements. In the simple case of adding one element at a time, this boils down to realigning at powers of 2, for a constant amortized cost.

The code could be more efficient, but there don't seem to be order of magnitude algorithmic improvements left.

  • Comment on Re: Re: Shift, Pop, Unshift and Push with Impunity!