June 2018
Intermediate to advanced
316 pages
6h 34m
English
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() ...
Read now
Unlock full access