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

Jacinta Richardson jarich at perltraining.com.au
Sat Nov 13 22:56:11 PST 2010

Sam Watkins wrote:

> With the perl method as you described it, perl would have to reallocate the
> array (or memmove its elements) while using it as queue, even if the total size
> of the queue was not increasing.

This is true, and I admit that my answer was only almost correct.  Queues are a 
very common data structure, and as you said, they would have a tendency to creep 
forward if what I said was completely correct.  Instead Perl's arrays are 
slightly biased towards queues and a preference to using C<push>; thus while I 
said the start and end indexes are set to the middle of the allocated memory, 
technically they're actually a bit closer to the start of the allocated memory. 
  An array grown exclusively with unshift will require more reallocations than 
one exclusively with push, and the difference results in the former being 
something like 5-10% slower than the later, (I've seen different benchmark 
results, but haven't tested it myself).  Since memory reallocations are rare 
compared to most of the other things you'll have going on in your program, it's 
safe to trust that arrays just grow magically without almost any cost at all.


More information about the Melbourne-pm mailing list