Friday, February 11, 2005

Java code is running faster than equivalent C++ code

I did not believe it at first, but it seems true: a fuzzy matching algorithm that I wrote is running faster implemented in Java than in C++ (about 5% faster).

A few caveates: the Java code is part of a large very long running server process so Hotspot has had a lot of time to optimize the code. Also, about 90% of the runtime of the entire system is in the small section of code that I reimplemented in C++ - this is about as good a scenario for Hotspot optimization than you could hope for. The particulars:

Java JVM: J2RE 1.4.2 IBM build for PowerPC.
C++: GNU g++ used with -O3

The code uses a packed block of 8 bit byte values that is about 4 megabytes. There is no code in this algorithm that could be optimized away: just some nested loops comparing byte values and performng a few sums.

Java on the server is not slow! This is for a customer project, and to get better performance I was willing to recode the whole thing in C++ if there had been an appreciable performance gain - obviously I won't do that now :-)

No comments: