Packing the keys will make barely any difference to the storage requirements of your hash as shown below. In the first case, a hash containing 2e6 x 50-byte keys takes 70MB. In the second case, 2e6 x 12-byte keys 67MB:
$hash{ sprintf "%050d", $_ } = 1 for 0 .. 2e6;;
print total_size \%hash;;
70183684
$hash{ sprintf "%012d", $_ } = 1 for 0 .. 2e6;;
print total_size \%hash;;
67660852
If all you want to do is store the strings so you can iterate over them, storing the uncompressed strings in an array will save far more space (30MB versus 67/70MB):
$a[ $_ ] = sprintf "%050d", $_ for 0 .. 2e6;;
print total_size \@a;;
30408448
You also save the cost of compressing and decompressing.
There are also other ways of storing and iterating your 2e6 strings that take even less memory but are just as easy and efficient to iterate.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|