This section provides a tutorial example on how to perform benchmark tests to find out how long each step will take to run in an empty loop in interpreted-only mode. The answer is 16 nanoseconds!
My first goal is to find out how long it will take to run an empty loop with an "int" index
in interpreted-only mode.
I used these parameters to control my tests:
"-Xint" JVM option to stop JIT compilations and keep my test in interpreted-only mode.
"-Xms100m -Xmx100m" JVM options to allocate enough memory to avoid GC.
"warmups=10000" benchmark runner parameter to ensure the JVM is warm enough to be stable.
"runs=100" benchmark runner parameter to repeat the test 100 times to average
out interruptions from the operating system.
"steps=10, 100, 1000, 10000, 100000, and 1000000" benchmark runner parameters
to see the impact of the loop overhead.
Here is how my empty loop test method look like:
/**
* BenchmarkTestInt.java
* Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
*/
class BenchmarkTestInt {
// The empty loop benchmark test method
public static int emptyLoop(int steps, BenchmarkRunner runner) {
int x = 0;
int i = 0;
int last = steps;
runner.startTimer();
for (i=0; i<last; i++) {
}
runner.stopTimer();
x = i;
return x;
}
}
The execution time of an empty "int" loop is about 16 nanoseconds per step in interpreted-only mode.
This conclusion can be arrived from test results of 10000, 100000 and 1000000 steps.
The execution time per step is probably the amount of time for incrementing the loop index
and checking loop termination condition.
If I convert the per step execution time into my CPU clock clicks, it will be: 16 / 0.5 = 32 clicks,
because my CPU clock speed is 2GHz = 0.5 nanoseconds per click.
The loop overhead is about 2000 to 2300 nanoseconds. This can calculated using test results of 10, 100 and 1000 steps.
For example, if the average execution time is 39 per step including overhead for 100 steps,
and we know the actual execution time per step is 16 nanoseconds,
so the overhead is: 39*100 - 16*100 = 2300 nanoseconds.
This test also confirms that the Java bytecode compiler "javac" is not doing any optimization
to replacing the empty loop with "i=steps" which is the net effect of the loop.
I also noticed that during the execution of the 1000000-step test,
my laptop CPU cooling fan ran continuously. The cooling fan stopped gradually after the test was done.
This tells me that my 1000000-step test was really CPU intensive and caused its temperature to raise!