Java Reference
In-Depth Information
THREADED MICROBENCHMARKS
The need to use a volatile variable in this example applies even when the microbenchmark is
single-threaded.
Be especially wary when thinking of writing a threaded microbenchmark. When several threads
are executing small bits of code, the potential for synchronization bottlenecks (and other thread
artifacts) is quite large. Results from threaded microbenchmarks often lead to spending a lot of
time optimizing away synchronization bottlenecks that will rarely appear in real code—at a cost
of addressing more pressing performance needs.
Consider the case of two threads calling a synchronized method in a microbenchmark. Because
the benchmark code is small, most of it will execute within that synchronized method. Even if
only 50% of the total microbenchmark is within the synchronized method, the odds that as few as
two threads will attempt to execute the synchronized method at the same time is quite high. The
benchmark will run quite slowly as a result, and as additional threads are added, the performance
issues caused by the increased contention will get even worse. The net is that the test ends up
measuring how the JVM handles contention rather than the goal of the microbenchmark.
Microbenchmarks must not include extraneous operations
Even then, potential pitfalls exist. This code performs only one operation: calculating the
50th Fibonacci number. A very smart compiler can figure that out and execute the loop only
once—or at least discard some of the iterations of the loop since those operations are redund-
ant.
Additionally, the performance of fibImpl(1000) is likely to be very different than the per-
formance of fibImpl(1) ; if the goal is to compare the performance of different implementa-
tions, then a range of input values must be considered.
To overcome that, the parameter passed to the fibImpl1() method must vary. The solution
is to use a random value, but that must also be done carefully.
The easy way to code the use of the random number generator is to process the loop as fol-
lows:
for
for ( int
int i = 0 ; i < nLoops ; i ++) {
l = fibImpl1 ( random . nextInteger ());
}
Search WWH ::




Custom Search