Sub topic 14
Reading score, error and units
This chapter assumes you can build and run a JMH jar and that you know what @Benchmark, @Param, forks and iterations configure. Here you learn to read the table JMH prints at the end of a run.
The result table
Running SumBenchmark from earlier with 2 forks and 5 measurement iterations each prints a summary like this:
Benchmark (size) Mode Cnt Score Error Units
SumBenchmark.sum 100 avgt 10 33.512 ± 0.412 ns/op
SumBenchmark.sum 10000 avgt 10 3012.874 ± 45.120 ns/opEach column answers one question:
| Column | Meaning |
|---|---|
| Benchmark | The method that ran. |
| (size) | The `@Param` value for this row. Each value gets its own row. |
| Mode | The benchmark mode. `avgt` is average time. |
| Cnt | The number of measurement iterations that were combined. |
| Score | The mean of those iterations. |
| Error | The uncertainty around the score. |
| Units | What the score measures. |
Cnt, score and error
Cnt is forks multiplied by measurement iterations. Here that is 2 × 5 = 10. Warmup iterations are not counted, because JMH discards them.
Score is the mean of the per-iteration results across all forks. It is not the best run and not the median.
Error is the half-width of a 99.9% confidence interval around the score. The first row therefore reads as "the true mean is probably between 33.100 and 33.924 ns/op". A small error relative to the score means the runs agreed. An error that is a large fraction of the score means the measurement is noisy. Typical causes are a busy machine, too few iterations, or only one fork.
Units and direction
The unit comes from the mode and from @OutputTimeUnit. Whether a bigger number is better depends on the mode.
| Mode | Typical unit | Better is |
|---|---|---|
| `Throughput` (`thrpt`) | ops/ns, ops/s | Higher |
| `AverageTime` (`avgt`) | ns/op, us/op | Lower |
| `SampleTime` (`sample`) | ns/op | Lower |
| `SingleShotTime` (`ss`) | ns/op | Lower |
An "op" is one invocation of the benchmark method. If your method loops over 10,000 elements, one op is the whole loop. The 3012.874 ns/op above is therefore the cost of summing all 10,000 elements, about 0.3 ns per element. The first row costs about 0.34 ns per element. Divide by the size before you compare per-element cost across sizes.
Comparing two results
Compare rows only when mode and units match. Then check whether the intervals overlap:
Benchmark Mode Cnt Score Error Units
Sort.arraysSort avgt 10 812.40 ± 15.30 ns/op
Sort.manualSort avgt 10 829.10 ± 20.70 ns/opThe ranges are 797.10 to 827.70 and 808.40 to 849.80. They overlap, so you can't claim arraysSort is faster. To tell them apart, add iterations or forks to shrink the error, or run on a quieter machine.
The error covers only the variation that JMH sampled. It does not cover a flawed benchmark. If the JIT removed your loop, you can get a tiny error around a meaningless score. A suspiciously fast score, such as a sort of a large array in 1 ns, usually signals dead-code elimination or constant folding rather than a fast method.
Common mistakes
- Comparing
thrptandavgtrows, or rows with different time units, without converting. - Reading the score of one
@Paramrow as if it applied to all sizes. - Trusting results from a single fork, since the error then reflects only one JIT history.
- Ignoring
Errorand comparing only the scores.
Exercise
A run shows A.run avgt 10 250.0 ± 30.0 ns/op and B.run avgt 10 270.0 ± 25.0 ns/op. Can you conclude A is faster than B? Explain.
Show answer
No. A's interval is 220 to 280 ns/op and B's is 245 to 295 ns/op. They overlap, so the difference is within the measurement noise. More iterations or forks, or a quieter machine, are needed before drawing a conclusion.