Java Benchmarks with JMH

Useful benchmarking is hard, but there are tools and patterns to help

Ben Weidig
The Startup
Published in
7 min readJun 10, 2020

--

Almost every developer knows the phrase “premature optimization is the root of all evil”, coined by Donald Knuth in 1974. But how are we supposed to know what is worthy of being optimized?

Our computing power improved manifold since then. But the sentiment to focus on the real issues for optimization efforts holds still true. Understanding the different kinds of latency and how to find actual relevant bottlenecks, and not only perceived ones, is the key to good benchmarking.

The Pareto Principle

It’s an axiom of business management, named after the Italian economists Vilfredo Pareto, stating

80% of sales come from 20% of clients.

This distribution applies to many different areas. In computer science, we can apply the principle to our optimization efforts. 80% of the actual work and time are done by 20% of code.

There might be low-hanging fruits for our optimization efforts in the 80%. But we should concentrate on the harder 20% if we want to make a real impact.

To identify the bottlenecks and optimization-worthy code, we need to benchmark the right things.

--

--

Ben Weidig
The Startup

Software developer, entrepreneur, blogger. Mostly Java, sometimes Swift, Golang, Bash and all the other fun stuff.