- Mastering High Performance with Kotlin
- Igor Kucherenko
- 308字
- 2021-06-25 20:55:25
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.
- Java多線程編程實(shí)戰(zhàn)指南:設(shè)計(jì)模式篇(第2版)
- TypeScript入門與實(shí)戰(zhàn)
- 摩登創(chuàng)客:與智能手機(jī)和平板電腦共舞
- 架構(gòu)不再難(全5冊)
- Machine Learning with R Cookbook(Second Edition)
- Java虛擬機(jī)字節(jié)碼:從入門到實(shí)戰(zhàn)
- Getting Started with NativeScript
- Visual C++開發(fā)入行真功夫
- 劍指大數(shù)據(jù):企業(yè)級數(shù)據(jù)倉庫項(xiàng)目實(shí)戰(zhàn)(在線教育版)
- Building Serverless Web Applications
- Maker基地嘉年華:玩轉(zhuǎn)樂動魔盒學(xué)Scratch
- TypeScript 2.x By Example
- C語言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)與習(xí)題精解
- JavaScript前端開發(fā)程序設(shè)計(jì)教程(微課版)
- JavaScript Security