in reply to Array vs. Hash (newbie perlguts)

As I understand it a hash is more expensive than an array. Two hashes are more expensive than one hash due to the cost of setting up the buckets. In your case you want to waste some elemets in the array so the answer is GOK. The solution is suck it and see. Run this sort of code and look at real time memory useage. Make either a hash or an array an look at the memory hit.

$size = 20000; # make a hash for ($key = 0 ; $key < $size ; $key += 2 ) { $hash{$key} = [ ( 0..100 ) ]; } # make an array, wasting 50% of the spaces #for ($key = 0 ; $key < $size ; $key += 2 ) { # $ary[$key] = [ ( 0..100 ) ]; #} print "Made big data structure, sleeping 20 seconds!\n"; sleep 20;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Array vs. Hash (newbie perlguts)
by Ryszard (Priest) on Aug 31, 2001 at 02:28 UTC
    I think the data structure with regard to memory allocation depends more on the sparcity of the data. ie an array will always have memory allocated to the upper bound, a hash will only have memory allocated when a cell is referenced (peeked or poked). I'm thinking 'complex data structures' when i say this (hoh, aoa, hoa et al..)

    am i correct on this or talking thru' my horse?