Sub topic 1
Why hand-written timing loops mislead
Prerequisites
This chapter assumes you know basic Java syntax and classes, and can compile and run a program from the command line. You should also know that the JVM compiles frequently used code while the program runs. Later chapters use annotations and Maven, but this one needs neither.
The tempting approach
The obvious way to measure code is to read the clock before and after a loop.
public class NaiveTiming {
static long sum(int[] data) {
long total = 0;
for (int value : data) {
total += value;
}
return total;
}
public static void main(String[] args) {
int[] data = new int[10_000];
for (int i = 0; i < data.length; i++) {
data[i] = i;
}
for (int round = 1; round <= 5; round++) {
long start = System.nanoTime();
for (int i = 0; i < 10_000; i++) {
sum(data);
}
long elapsed = System.nanoTime() - start;
System.out.println("round " + round + ": " + elapsed / 10_000 + " ns per call");
}
}
}Run it a few times. The five rounds in one run usually differ, and the numbers change from run to run. Nothing in the program tells you which number is the real cost of sum.
What goes wrong
The JVM does not execute your bytecode at a constant speed. Several effects distort a hand-written loop.
- Interpreter first, compiled code later. The JVM starts by interpreting bytecode. The JIT (just-in-time) compiler translates hot methods into optimized machine code after they have run many times. Early rounds measure the interpreter, and later rounds measure compiled code.
- Compilation happens during your measurement. The JIT works in background threads, and it may replace a method partway through your timed loop. You then time a mixture of slow and fast code.
- Dead-code elimination. The call
sum(data)discards its result. Once the JIT sees that the call has no visible effect, it may remove the work, and you time an empty loop. - Constant folding. If the input is a constant, the JIT can compute the answer at compile time, and the loop measures nothing.
- Noise. Garbage collection pauses, other processes, CPU frequency changes and clock granularity all add to a single sample.
- Shared profile. The JIT specializes code based on what it has seen so far. Benchmarks in the same JVM can change each other's results.
Common mistakes
- Timing a single run and treating it as the answer.
- Ignoring the first rounds, or averaging them with later ones.
- Calling a method and throwing away its result.
- Using constants as input.
- Comparing two implementations in one JVM run, where the second inherits state from the first.
You can reduce some of these problems by hand. You can add warmup rounds, print the result, and run the program several times. Doing this correctly for every case is tedious and error-prone, and that is the problem JMH was written to solve. The next chapters look at warmup more closely and then introduce JMH.
Exercise
In the loop above, sum(data) is called and its return value is ignored. Name two ways this can make the measurement misleading.
Show answer
The JIT may see that the call has no visible effect and remove it (dead-code elimination), so the loop measures almost nothing. Also, the first rounds run in the interpreter before the JIT compiles sum, so the early timings are much slower than the later ones. Averaging them gives a number that describes neither.