- 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.
- PHP動態(tài)網站程序設計
- Spring 5.0 Microservices(Second Edition)
- 零起步玩轉掌控板與Mind+
- Android NDK Beginner’s Guide
- Learning ArcGIS Pro
- Internet of Things with Intel Galileo
- Kinect for Windows SDK Programming Guide
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(1)
- 青少年信息學競賽
- Visualforce Developer’s guide
- Android Studio Cookbook
- 從零開始學Android開發(fā)
- JSP程序設計與案例實戰(zhàn)(慕課版)
- Beginning C# 7 Hands-On:The Core Language
- Learning WordPress REST API