|P is for Practical|
Array vs. Hash for sparsely integer-indexed databy puterboy (Scribe)
|on Jan 25, 2013 at 18:41 UTC||Need Help??|
puterboy has asked for the
wisdom of the Perl Monks concerning the following question:
My perl program frequently needs to retrieve a cached set of data that is indexed by a positive but sparsely populated set of integers.
I am not sure whether it is better to use a hash vs. an array when considering efficiency of storage and efficiency of lookup speed.
I know in languages like 'C' you would need to declare the full range of index possibilities which would be space-inefficient for sparsely indexed data.
On the other hand, I am concerned that maybe a hash would be slower when the keys are known to all be positive integers.
In particular, I am asking:
1. What (if any) storage inefficiency (relative to hashes) is created in perl when using arrays for sparsely indexed data?
2. What (if any) lookup inefficiency (relative to arrays) is created in perl when using hashes for positive integer indexed keys?