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

Alfie John alfiejohn at gmail.com
Thu Nov 11 00:38:59 PST 2010


On Thu, Nov 11, 2010 at 5:16 PM, Daniel Pittman <daniel at rimspace.net> wrote:

> >>> Hmm, interesting to hear.
> >>> So, the array methods are quicker than hashes for small sets?
> >>
> >> They should be faster than a hash for *any* set, because they are a
> constant
> >> time access with zero computation, rather than a just-above-constant
> time
> >> access with some computation.
> >
> > I was confused by how it could be computation-free for a moment, then
> realised
> > you're talking about accessing things by doing $planets[$id], which of
> course
> > is quicker than $planets->{$id}.
>
> I was talking about the more general case of array vs hash, where access is
> zero-cost "index times size" vs some-cost "index through hash, then walk
> past
> collisions".  (Presumably your comment about speed wasn't quite so
> restricted. :)
>

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()!

If you are reading and writing to an array of fixed size, then yes it's zero
cost. Otherwise, know the size upfront and do something like:

  my @array; $array[MAX_SIZE-1] = undef;

Alfie


>        Daniel
>
> --
> ✣ Daniel Pittman            ✉ daniel at rimspace.net            ☎ +61 401 155
> 707
>               ♽ made with 100 percent post-consumer electrons
> _______________________________________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20101111/9976b215/attachment.html>


More information about the Melbourne-pm mailing list