官术网_书友最值得收藏!

Using black holes

You can use an instance of Blackhole explicitly, but it's really useful if you're going to consume several values with the black hole. In another case, this approach just affects readability and it would be better to just return the value. The next example demonstrates correct and incorrect cases:

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class MyBenchmark {

double x1 = Math.PI;
double x2 = Math.PI * 2;

@Benchmark
public double baseline() {
return Math.log(x1);
}

@Benchmark
public double measureWrong() {
Math.log(x1);
return Math.log(x2);
}

@Benchmark
public double measureRight_1() {
return Math.log(x1) + Math.log(x2);
}

@Benchmark
public void measureRight_2(Blackhole bh) {
bh.consume(Math.log(x1));
bh.consume(Math.log(x2));
}

}

The output shows how the JVM eliminates the first line of the measureWrong() method:

 Benchmark                         Mode  Cnt Score Error Units
MyBenchmark.baseline avgt 5 24.385 ± 1.559 ns/op
MyBenchmark.measureRight_1 avgt 5 43.861 ± 4.813 ns/op
MyBenchmark.measureRight_2 avgt 5 47.041 ± 4.800 ns/op
MyBenchmark.measureWrong avgt 5 24.447 ± 2.333 ns/op
主站蜘蛛池模板: 保山市| 赤峰市| 安化县| 高青县| 册亨县| 东阿县| 太康县| 松滋市| 垦利县| 滨海县| 周至县| 济阳县| 石狮市| 神池县| 黎平县| 肥城市| 高碑店市| 酒泉市| 团风县| 溧阳市| 江川县| 宣武区| 句容市| 乐安县| 永善县| 怀远县| 张掖市| 卢龙县| 修武县| 揭东县| 千阳县| 会理县| 隆昌县| 黄平县| 金川县| 伊宁市| 岢岚县| 清徐县| 新泰市| 如皋市| 渭南市|