0

I have a question. I have to process IP addresses and port numbers. Each packet contains source and destination IP, source and destination port. Let say a packet is (1.2.3.4, 5.6.7.8,22, 8080) So I can store them in two different data types.

 String packet ="1.2.3.4, 5.6.7.8,22, 8080";
 short[] packet={1,2,3,4,5,6,7,8,22, 8080}

Which one will take more memory and which one is more efficient for processing

user3212493
  • 165
  • 2
  • 11
  • 3
    Will you be creating millions of these? If not, don't worry about memory. – user949300 Oct 15 '14 at 04:10
  • 1
    Depends on what kind of processing you want. Absent that, I'd probably use a class which wraps two `java.net.InetAddress` instances and two `unsigned short`s, as it gives you best readability and versatility. Optimise if you find you need to optimise. – Amadan Oct 15 '14 at 04:10
  • Yes I have to process millions of records therefore memory and efficiency both are primary concerns – user3212493 Oct 15 '14 at 04:12
  • I think the java.net.InetAddress will take more space than using either string or short – user3212493 Oct 15 '14 at 04:27
  • @Amadan Java doesn't have unsigned types, and a `short` will usually end up stored as 32 bits anyway. – chrylis -cautiouslyoptimistic- Oct 15 '14 at 04:27
  • 1
    This is the worst sort of premature optimization. *Use the proper class* and only go back if it turns out that there's an *actual* problem. "Millions" of records is nothing in the age of gigabytes of memory on cell phones. – chrylis -cautiouslyoptimistic- Oct 15 '14 at 04:28
  • 1
    @chrylis: Derp. :) But yeah, premature optimisation. I believe (not 100% sure) that `java.net.Inet4Address` only stores a 32-bit number. Overhead might get you over the `short[]` size, but `short[]` would be harder to process (again, depending what processing you're doing). And besides, for certain types of processing, you don't need to have everything in memory at once. – Amadan Oct 15 '14 at 04:33

2 Answers2

0

The short[] is 10*2 bytes, plus object overhead.

The String requires a delimiter, so it is at least 9*2 + 1 chars (if every number is a single digit) plus object overhead, and in realistic cases will be always longer.

So the short[] is both less memory and (I strongly suspect) easier to process.

user949300
  • 15,364
  • 7
  • 35
  • 66
0

In reality, each IP carries 32-bits of data (4 8-bit values) and each port carries 16-bits of data. So your total record can be compressed into a total of 32+32+16+16 = 96 bits, or 3 32-bit int primitives.

You can use the answer from this question: Going from 127.0.0.1 to 2130706433, and back again to pack each IP address into a single int value. A similar technique can pack the two port numbers into a single int as well (since port numbers are are really up to 16-bit values).

With this "optimization", you can store a single record into 3 int values (which you could wrap into a class and provide utility methods to get each piece of data).

Community
  • 1
  • 1
xpa1492
  • 1,953
  • 1
  • 10
  • 19
  • Nice way to save a few bytes. Note that Java pads most objects to a multiple of 8 bytes (details vary) so you'd need to work out the details to see if this really saves or not. – user949300 Oct 15 '14 at 19:38