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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.