Using PerformanceTestResult helpers
A performance helper suite of classes was introduced, allowing "shoot-out"s like the above to be more simple:
Callable<Void> exprCallable = new Callable<Void>() {
@Override
public Void call() throws Exception {
expression.value(context);
return null;
}
};
Callable<Void> javaCallable = new Callable<Void>() {
@Override
public Void call() throws Exception {
function(x);
return null;
}
};
PerformanceTestCallable exprPerfCallable = new PerformanceTestCallable("Expression "
+ expression.getExpressionString(), exprCallable);
PerformanceTestCallable javaPerfCallable = new PerformanceTestCallable("Java", javaCallable);
PerformanceTestResult compareResult = PerformanceTestHelper.compare(20, 200000, exprPerfCallable,
javaPerfCallable);
compareResult.printResults();
The API is com.splwg.base.api.testers.performance.PerformanceTestHelper:
public static PerformanceTestResult compare(int warmups, int reps, PerformanceTestCallable... callables)
throws Exception {
Each of the performance callables is treated the same. It gets a series of warmup executions, in order to populate caches, and allow hotspot JVM optimizations of any methods. Then the accurate system nano timing (e.g., System.nanoTime()) is called around the loop of the given number of reps.