[Melbourne-pm] Timer::HiRes and alarms?

Jacinta Richardson jarich at perltraining.com.au
Thu Nov 11 16:05:57 PST 2010

Alfie John wrote:

> Be careful to think that this is absolutely true. Perl's arrays are not 
> C arrays. If your index is greater than the number of elements in the 
> array, then the array will be extended i.e. the array element pointers 
> will be copied into a new array which can hold just one more element. 
> This will happen *each time* you extend the size of an array i.e. push()!

This is not completely correct.  When you create an array in Perl, Perl creates 
a hunk of memory for it and puts the array in the middle.  If you unshift or 
push things onto the array they bring the start or end of the array closer to 
the boundaries of the memory Perl put aside for you.  If you get "too close" to 
either end, then Perl allocates twice as much space for you (as it did the first 
time) and copies your array into the middle of that.  You can then add to the 
start or end of the array at will until you again get "too close" to one of the 
ends; wherein Perl gives you more space again.

So it is just as cheap to unshift onto your array as to push; and the majority 
of additions to your array do not require Perl to do a full array copy.


More information about the Melbourne-pm mailing list