[Israel.pm] unshift vs push&reverse

Yitzchak Scott-Thoennes sthoenna at efn.org
Thu Mar 26 18:01:10 PDT 2009


On Thu, March 26, 2009 2:29 am, Gabor Szabo wrote:
> ps. push should be way faster than unshift but I am quite sure it does not
>  matter in your code. If you want to check, benchmark them.

No, perl is smart enough to make unshift just about as fast as push.

The array of pointers that a Perl array is based on can have extra space
at the beginning, and when an unshift needs to reallocate the array, it
grabs extra space based on the number of elements in the array, such that
N consecutive unshifts should require at most log(N)/log(2)+1 reallocations.



More information about the Perl mailing list