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

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.

主站蜘蛛池模板: 江油市| 仙居县| 承德市| 桂林市| 平安县| 班玛县| 新密市| 长乐市| 板桥市| 绵竹市| 鄄城县| 大连市| 天津市| 礼泉县| 石台县| 平山县| 女性| 万源市| 温州市| 道孚县| 定襄县| 丹棱县| 家居| 淮安市| 柳州市| 绍兴县| 英德市| 叶城县| 尉犁县| 斗六市| 康乐县| 海晏县| 灵川县| 江山市| 托克托县| 肥东县| 巴林右旗| 平顺县| 呼图壁县| 堆龙德庆县| 合阳县|