Friday, November 13, 2015

Benchmarks in Java (Know what u're measuring)

Accurate Benchmark

It's often needed to know whether the certain operation is faster than another one(especially to argue with somebody on code review:)).
Probably the most of the fast self-written benchmark tests are incorrect because of side factors, that were not considered.
Here are few points we have to consider before start doing better:
  • JVM warmup at start an execution is slow and becomes faster and faster until it goes to steady-state;
  • Just In Time Compiler optimizes the code, so your measures become wrong;
  • Garbage Collector GC could act during your measure so it'll affect the results;
  • Class loading First execution time usually includes the loading of all classes it uses, cause JVM typically loads classes only when they're first used;
  • False Sharing Might happen when u try to read 2 adjacent fields from 2 or more threads at the same time (those could contend with each other on the hardware level)
  • Caching CPU cache, File system cache, Memory access optimizations.

There is a fairly simple and useful Code Tool by Open JDK called JMH supposed to simplify & optimize the benchmarks writing & running (references are in the end of page).

No comments:

Post a Comment