in reply to are perl hash keys always strings?
If the string representation of your integers is longer than their packed representation, then you will save a few bytes per key, but the savings will be minimal. A few hundred kilobytes at most:
undef $h{ $_ } for 1e6 .. 2e6;; print total_size( \%h );; 34617643 undef $i{ pack 'V', $_ } for 1e6 .. 2e6;; print total_size( \%i );; 34231634 undef $j{ $_ } for 100e6 .. 101e6;; print total_size( \%j );; 34883144 undef $k{ pack 'V', $_ } for 100e6 .. 101e6;; print total_size( \%k );; 34239854
And the relative saving is less still if you are assigning values to the keys.
If you described your application and the nature/distribution of your keys in more detail, there are often ways of storing such data that has significantly lower memory costs. Though these usually trade space for speed.
|
|---|