Fastest algorithm to append a id to an ipaddress depending upon the net id
807588Jun 10 2009 — edited Jun 10 2009Hi Guys,
I wanted to know what will be the fastest algorithm to append a id to an ip address depending on the net id. I am using Java by the way. For example - Suppose I have a list of ids and corresponding net id i.e.
ID: 23 net ID: 10.44
ID:12 net ID: 10.56
ID:1 net ID: 10.70
.............................
.............................
ID: 78 net ID: 10.44.34.33
Now, if I receive a million records having different source IPs, what will be the fastest way to know to which ID it belongs. For example, say I receive a record having source IP 10.44.23.33 then I have to compare to all the net IDs and see what ID it will belong to; in this case, result need to be -> 23::10.44.23.33 because net ID:10.44 has ID 23. Suppose, I get a record having source IP 10.44.34.33 then result need to be -> 78::10.44.34.33. I hope, I am clear. Performance issue is if I have million records and list comprising say even 1000 ID and net ID entries then if I do say, compare the source IP to each entry of the list then searching will take O(n). Not sure though. I know, I can convert net ID to the range of IP addresses. Take ID:12 for example. The number of IP addresses that can belong to that network type will be from 10.56.0.0 to 10.56.255.255. Convert these numbers to long and convert the source IP also to long and see if it is between the range. But, this will take a long time even if I do binary search, am I right? Is there any way I can resolve the issue in a nifty way, say, using binary manipulation such as ORing or ANDing something. Would anyone please let me know? Thanks.
Waiting for your replies.
Edited by: wannabehacker on Jun 10, 2009 8:06 AM
Edited by: wannabehacker on Jun 10, 2009 8:07 AM