JDK 1.6 Compiler Optimization performance difference between IP address to long implementations
By Angsuman Chakraborty, Gaea News NetworkWednesday, August 5, 2009
Just to give all ya techies some food for real geeky thoughts, here is a kind of an eye opener of how much JDK 1.6 compiler has optimized itself over the years. Our developers were working on a project that needed IP addresses to get simplified and be represented by numbers. Understandably it reduces a lot of overheads from the server as a single IP address generally takes up 4 bytes. Now in lieu of using a common code, we decided to optimize it our way and improve some performance. So here goes the original code that perhaps you have used too
public static Long ip2long(String addr) {
String[] ipArray = addr.split(”\\.”);
if(ipArray.length != 4) return null;
return Integer.parseInt(ipArray[3]) + Integer.parseInt(ipArray[2]) * 256 + Integer.parseInt(ipArray[1]) * 65536 + Integer.parseInt(ipArray[0]) * 16777216L;
}
and here is ours
public static Long ip2long2(String addr) {
String[] ipArray = addr.split(”\\.”);
if(ipArray.length != 4) return null;
long num = 0;
for (int i=0;i<ipArray.length;i++) {
int power = 3-i;
num += ((Integer.parseInt(ipArray[i]) % 256 * Math.pow(256, power)));
}
return num;
}
Now can you guess the significant performance difference?
In our tests the performance difference is 400 picoseconds per iteration on a Core2Duo 2.4Ghz computer! In other words 200 milliseconds over 500000 iterations. The test code has been written to prevent unwanted compiler optimizations affecting the result. The results are repeatable and has been calculated after discarding the first set. The performance difference is identical in client as well as server VM mode.
So we rest our case here. Give us your insights.
Tags: JDK 1.6, JDK 1.6 compiler, JDK 1.6 optimization
david shay