The benchmarks and stopwatch programs of the HappyCoders.eu article on the Java Microbenchmark Harness (JMH). The article is in preparation; the links follow with its publication.
The project is set up the way the JMH Maven archetype generates it, with the one addition JDK 23 and newer need: the annotation processor jmh-generator-annprocess is named in the annotationProcessorPaths of the compiler plugin.
The sources need Java 25 or newer. Build the benchmark JAR:
mvn clean package
List the benchmarks, then run one of them – the argument is a regular expression matched against package, class and method name:
java -jar target/benchmarks.jar -l
java -jar target/benchmarks.jar 'eu\.happycoders\.jmh\.IntToStringBenchmark'
IntToStringBenchmark runs with the JMH defaults and takes 8 minutes and 20 seconds; the other benchmarks set shorter runs in their annotations.
All in the package eu.happycoders.jmh:
IntToStringBenchmark– the first benchmark:Integer.toString()in one method, everything else left to the JMH defaultsBaselineBenchmark– an empty method and one that returns its input: what a call through the harness costsDeadCodeBenchmark– a result that is dropped, returned, or handed to aBlackhole, forMath.log()and forInteger.toString()ConstantFoldingBenchmark– a constant as input against a non-final field, forInteger.parseInt()and forInteger.toString()LoopBenchmark– one conversion per call against a loop in the benchmark method with@OperationsPerInvocationSortedDataBenchmark– a loop with a branch over sorted and over shuffled numbers, for a thousand and for a million elementsSingleTypeBenchmark–toString()on an array that holds one type against an array that mixes threeReadWriteBenchmark– three threads read anAtomicLongwhile a fourth increments it, with@Groupand@GroupThreadsBatchSizeBenchmark– appending to anArrayListfor one second per iteration againstSingleShotTimewith a batch size of 10,000
BenchmarkRunner starts IntToStringBenchmark from a main() method with JMH's OptionsBuilder, the way to run a benchmark from the IDE.
The package eu.happycoders.jmh.stopwatch holds what the article measures without JMH, with System.nanoTime():
ClockResolution– the step size ofSystem.nanoTime()on the machine it runs onStopwatch– a million conversions, timed onceStopwatchRounds– the same measurement thirty times in one JVMStopwatchLogarithm– a million logarithms per round, added upStopwatchUnusedResult– the same loop without anybody using the logarithmsStopwatchTwoCandidates– two ways to convert, one after the other; with the argumentreversein the opposite order
Run them after mvn compile, for example:
java -cp target/classes eu.happycoders.jmh.stopwatch.Stopwatch
results/m5-pro-jdk27/ holds the logs and JSON files of the runs the article quotes, measured on JDK 27 on an Apple M5 Pro, with a summary in its README.md.
Stay up-to-date with the latest Java features with this free PDF Cheat Sheet!
- Avoid lengthy research with this concise overview of all Java versions from Java 10 to Java 27.
- Discover the innovative features of each new Java version, summarized on a single page.
- Impress your team with your up-to-date knowledge of the latest Java version.
👉 Download the free Java Versions PDF
(Hier geht's zur deutschen Version → Java-Versionen PDF)
With this free 1-page PDF cheat sheet, you'll always have the 7 most important complexity classes at a glance.
- Always choose the most efficient data structures and thus increase the performance of your applications.
- Be prepared for technical interviews and confidently present your algorithm knowledge.
- Become a sought-after problem solver and be known for systematically tackling complex problems.
👉 Download the free Big O Cheat Sheet
(Hier geht's zur deutschen Version → O-Notation Cheat Sheet)
👉 Want to stay on top of modern Java? Sign up for the HappyCoders newsletter – Modern Java: new versions & features, performance, and JVM insights – once a month.
(Hier geht's zur deutschen Version → HappyCoders-Newsletter deutsch)
🇩🇪 An alle Java-Entwickler:innen, die durch fundierte Kenntnisse über Datenstrukturen besseren Code schreiben wollen
Trage dich jetzt unverbindlich auf die Warteliste von „Mastering Data Structures in Java“ ein, und erhalte das beste Angebot!


