Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Array vs. Hash for sparsely integer-indexed data

by flexvault (Parson)
on Jan 25, 2013 at 19:11 UTC ( #1015392=note: print w/ replies, xml ) Need Help??


in reply to Array vs. Hash for sparsely integer-indexed data

puterboy,

    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?

I'm sure you'll get more technical answers, but I use hashes now even when the indexing is sequential. The ability to use 'defined' or 'exists' to see if the key is present greatly improves the speed of lookups. And think of the storage benefits, since if you do the following:

my @array; $array[100] = 246; my %hash; $hash{"100"} = 246;

The array will use 101 ( 0..100 ) locations and the hash will allocate 8 (hashes are allocated in powers of 2).

But why not 'benchmark' it for yourself for your exact situation and know for sure :-)

Good Luck...Ed

"Well done is better than well said." - Benjamin Franklin


Comment on Re: Array vs. Hash for sparsely integer-indexed data
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015392]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-08-01 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (16 votes), past polls