This section describes what is Java micro benchmark and general guidelines on writing micro benchmark programs.
What Is Micro Benchmark?
Micro benchmark is a benchmark designed to measure the performance of a very small and specific piece of code.
One good example of Java Micro Benchmark programs is the Java Microbenchmark Applet maintained by
Jonathan Hardwick at http://www.cs.cmu.edu/~jch/java/microbench.html. It is designed to measure
the performance of Java basic operations like:
Loop overhead: while (Go) n++
Local variable assignment: i = n
Instance var. assign.: this.i = n
Array element assign.: a[0] = n
Byte increment: b++
Short increment: s++
Int increment: i++
Long increment: l++
Float increment: f++
Double increment: d++
Object creation: new Object()
Array creation: new int[10]
Method call: null_func()
Synchronous call: sync_func()
Math function: abs()
Inline code: (x < 0) ? -x : x
If you want to write your own micro benchmark programs, you need to consider:
Compiler optimization - Compiler may modify your code for optimization. For example,
expression "Hello " + "Herong!" will probably be optimized as "Hello Herong!".
You need to write your code carefully to avoid compiler optimization.
Java Virtual Machine (JVM) warm up - When JVM is started, it runs your application
thread together with other threads to warm up the system. If you run your benchmark test
immediately as after JVM started, warm up threads may affect your test code performance.
You need to give JVM a warm up period before starting your benchmark test code.
Garbage Collection (GC) - GC thread may affect your test code performance.
You need to avoid garbage collection or turn off garbage collection if possible.
Just In Time (JIT) compilation - Some JVM like HotSpot identifies code hot spots
and compile them into native code to speedup execution. If you want to test Java class
code interpretation speed, you should turn off JIT compilation. If you want to test native
code speed, you should JIT compilation of your test code first.
Class loading and initialization - Be aware class loading and initialization effects
on your test code. You need to load all classes before starting your benchmark test code.
Class loading and initialization - Be aware class loading and initialization effects
on your test code. You need to load all classes before starting your benchmark test code.
Environment noise - Applications other than the JVM running on your computer may affect
your test code performance. You need to turn off other applications, like virus scanner,
Yahoo messenger, etc., as much as possible on your computer before running your benchmark test code.