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

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
主站蜘蛛池模板: 铅山县| 马尔康县| 福清市| 柳林县| 宾阳县| 达拉特旗| 久治县| 和林格尔县| 岳普湖县| 安阳县| 白银市| 平度市| 织金县| 固阳县| 柳林县| 邯郸县| 全椒县| 泗洪县| 南汇区| 梁山县| 肇庆市| 汉源县| 久治县| 西乌珠穆沁旗| 邛崃市| 芒康县| 玉田县| 宁河县| 曲阳县| 永登县| 墨脱县| 丹寨县| 宣恩县| 明溪县| 耿马| 阿图什市| 东山县| 镇沅| 嘉定区| 临洮县| 舟山市|