- Code: Select all
public class Test {
public static void main(String [] args) {
long start = System.currentTimeMillis();
int p = 0;
for (int i = 0; i < 1000000000; i++) {
p += i;
}
long end = System.currentTimeMillis();
System.out.println(end - start + ":" + p);
}
}
IcedTea JVM (default in Fedora 8)
- Code: Select all
java version "1.7.0"
IcedTea Runtime Environment (build 1.7.0-b21)
IcedTea Server VM (build 1.7.0-b21, mixed mode)
This is a RedHat project to fill in the missing bits in Sun's OpenJDK project. It uses the same HotSpot mixed-mode interpreter as the Sun version.
Results:
- Code: Select all
512:-1243309312
(So it took 512 milliseconds to do all that)
GIJ JVM
- Code: Select all
java version "1.5.0"
gij (GNU libgcj) version 4.1.2 20070925 (Red Hat 4.1.2-33)
This appears to be just a bytecode interpreter with no 'Just-in-time' compiler.
Results:
- Code: Select all
30042:-1243309312
It took 30 seconds, which confirms the lack of a JIT compiler.
Sun's JVM
- Code: Select all
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Server VM (build 1.5.0-b64, mixed mode)
Results:
- Code: Select all
509:-1243309312
GCJ Compiler
This is the GNU Java compiler that can compile either to bytecode or native code. I'll choose the native code option.
To compile this to native, I issue the following command:
- Code: Select all
gcj -march=core2 -O3 --main=Test -o Test Test.java
Results:
- Code: Select all
466:-1243309312
Looks like native code is quite a bit faster. But then, there's more. Remember to hold your bladder in
- Code: Select all
gcj -march=core2 -funroll-loops -O3 --main=Test -o Test Test.java
Results:
- Code: Select all
58:-1243309312
Of course there is nothing surprising here since this benchmark is a pure loop. But note that the bytecode-based JVMs aren't that efficient with loop optimisation. So if you have lots of loops, then native code is probably the way to go.

