Ben Lavery wrote:
> there must be a slight trade-off... the processing required to
> initialize the hash table with it's keys and values is probably
> more intensive than defining an array with its respective values?
> Unless, internally, Perl stores arrays as hashes, with the indexes
> as the keys.

 I would have thought there would be more of a hit for initialising
 hashes over arrays, but the gains in efficiency later would outweigh
 that hit I'd have though?

Initially I was going to ask: "Comparing hashes to arrays, at what scale is the extra time required for hash initialization offset by the decreased time in searching? 10 items? 100? 1000?" But, I realized that the problem isn't that simple, because it depends on the operations you perform on the hash after you initialize it.

I get the impression (from reading this mailing list and other sources) that in most cases, the performance hit of initialization is relatively minor, compared to the performance gains later on. In other words, if there is a relatively direct (or natural) way to put your data into a hash instead of an array, it's worth it. And for some situations, if the values are unique, to define a mirrored hash (value => key) so that it's faster to search for values as key lookups instead.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to