June 2018
Intermediate to advanced
316 pages
6h 34m
English
Another downfall of benchmarks is Dead Code Elimination (DCE). The JVM reduces computations that are redundant or eliminates them completely. Let's come back to our first implementation of the testMethod() method:
@Benchmarkpublic void testMethod() { int a = 3; int b = 4; int c = a + b;}
In this example, the last line will be eliminated, but it's a significant part of our benchmark. The JMH provides the essential infrastructure to fight this issue. We can just return the result of computation as follows:
@Benchmarkpublic int testMethod() { int a = 3; int b = 4; return a + b;}
The returned result is implicitly consumed by black holes.
Read now
Unlock full access