If the number of keys is the cause of the memory cunsumption, make the keys smaller and more efficient. It might matter if you are low on memory. Best reason to pack the IP's is the it is much easier to sort
use List::Util qw( shuffle );
use Devel::Size qw( total_size );
my ($x, %ip0, %ip1, %ip2, %ip3, %ip4, %ip5) = 31;
foreach my $i0 ((shuffle 1..254)[0..$x]) {
foreach my $i1 ((shuffle 1..254)[0..$x]) {
foreach my $i2 ((shuffle 1..254)[0..$x]) {
foreach my $i3 ((shuffle 1..254)[0..$x]) {
$ip0{"$i0.$i1.$i2.$i3"}++;
$ip1{"$i0.$i1"}{"$i2.$i3"}++;
$ip2{$i0}{$i1}{$i2}{$i3}++;
$ip3{pack "CCCC", $i0, $i1, $i2, $i3}++;
$ip4{pack "CC", $i0, $i1}{pack "CC", $i2, $i3}++;
$ip5{pack "C", $i0}{pack "C", $i1}{pack "C", $i2}{pack "C", $i3}++
+;
}}}}
printf "{192.168.101.208} : %9d\n", total_size (\%ip0);
printf "{192.168}{101.208} : %9d\n", total_size (\%ip1);
printf "{192}{168}{101}{208} : %9d\n", total_size (\%ip2);
printf "{xC0xA8x65xD0} : %9d\n", total_size (\%ip3);
printf "{xC0xA8}{x65xD0} : %9d\n", total_size (\%ip4);
printf "{xC0}{xA8}{x65}{xD0} : %9d\n", total_size (\%ip5);
==>
{192.168.101.208} : 116483356
{192.168}{101.208} : 69879492
{192}{168}{101}{208} : 71176834
{xC0xA8x65xD0} : 106954864
{xC0xA8}{x65xD0} : 69611776
{xC0}{xA8}{x65}{xD0} : 71176690
On my machine using perl-5.28.0, $ip4{pack "CC", $i0, $i1}{pack "CC", $i2, $i3} proves to be the most memory-friendly approach
Enjoy, Have FUN! H.Merijn