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

The pitfalls of loops

There are a lot of optimizations related to loops. Let's investigate one more JMH example (http://hg.openjdk.java.net/code-tools/jmh/file/ef50cc696984/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_11_Loops.java), which measures how much time it takes to sum two integers:

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

int x = 1;
int y = 2;

@Benchmark
public int measureRight() {
return (x + y);
}

private int reps(int reps) {
int s = 0;
for (int i = 0; i < reps; i++) {
s += (x + y);
}
return s;
}

@Benchmark
@OperationsPerInvocation(1)
public int measureWrong_1() {
return reps(1);
}

@Benchmark
@OperationsPerInvocation(10)
public int measureWrong_10() {
return reps(10);
}

@Benchmark
@OperationsPerInvocation(100)
public int measureWrong_100() {
return reps(100);
}

@Benchmark
@OperationsPerInvocation(1000)
public int measureWrong_1000() {
return reps(1000);
}

@Benchmark
@OperationsPerInvocation(10000)
public int measureWrong_10000() {
return reps(10000);
}

@Benchmark
@OperationsPerInvocation(100000)
public int measureWrong_100000() {
return reps(100000);
}

}

The measureRight() method represents a case of correct measurement using the JMH. The reps method is an example of a simplified naive use of the JMH to measure a loop. The measureWrong_* methods represent wrong measurements with different repetition counts. The @OperationsPerInvocation annotation is used to get the inpidual operation cost. 

In the output, you may notice that the larger the repetition count, the lower the measured cost of the operation:

Benchmark                         Mode Cnt Score Error Units
MyBenchmark.measureRight avgt 5 3.531 ± 6.938 ns/op
MyBenchmark.measureWrong_1 avgt 5 2.695 ± 0.365 ns/op
MyBenchmark.measureWrong_10 avgt 5 0.297 ± 0.047 ns/op
MyBenchmark.measureWrong_100 avgt 5 0.033 ± 0.002 ns/op
MyBenchmark.measureWrong_1000 avgt 5 0.030 ± 0.002 ns/op
MyBenchmark.measureWrong_10000 avgt 5 0.025 ± 0.003 ns/op
MyBenchmark.measureWrong_100000 avgt 5 0.022 ± 0.002 ns/op

This happens because the loop is heavily pipelined, and the operation to be measured is hosted from the loop. This means that the compiler optimizes the code and calculates the final result without a loop.

主站蜘蛛池模板: 漠河县| 汽车| 无锡市| 永和县| 洮南市| 新乐市| 耿马| 贵德县| 大荔县| 舞钢市| 大庆市| 亚东县| 南丰县| 华阴市| 淮阳县| 旅游| 云浮市| 赤峰市| 张掖市| 广宁县| 博罗县| 乐东| 信丰县| 东乡| 沙河市| 舞阳县| 中卫市| 灵寿县| 望奎县| 远安县| 息烽县| 手游| 芜湖县| 绵阳市| 会泽县| 铜梁县| 东兰县| 库尔勒市| 武平县| 濮阳市| 十堰市|