Skip to content

Instantly share code, notes, and snippets.

@shipilev
Last active August 29, 2015 13:57
Show Gist options
  • Save shipilev/9354757 to your computer and use it in GitHub Desktop.
Save shipilev/9354757 to your computer and use it in GitHub Desktop.
Pop quiz: what's wrong with measureWrong()?
package org.sample;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.logic.BlackHole;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Measurement(iterations = 5)
@Warmup(iterations = 5)
@Fork(3)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class IterationBenchmark {
@Param({"1", "2", "3", "4", "5", "6"})
public int sizeOrder;
private List<Integer> list;
@Setup
public void setup() {
list = new ArrayList<Integer>();
for (int c = 0; c < Math.pow(10, sizeOrder); c++) {
list.add(10000 + c);
}
}
@GenerateMicroBenchmark
public void measureWrong() {
for (Integer i : list) {}
}
@GenerateMicroBenchmark
public void measureRight(BlackHole bh) {
for (Integer i : list) {
bh.consume(i);
}
}
}
Linux, x86_64, JDK 7u40:
Benchmark (sizeOrder) Mode Samples Mean Mean error Units
o.s.IterationBenchmark.measureRight 1 avgt 15 52.578 1.279 ns/op
o.s.IterationBenchmark.measureRight 2 avgt 15 500.127 27.728 ns/op
o.s.IterationBenchmark.measureRight 3 avgt 15 4762.948 247.602 ns/op
o.s.IterationBenchmark.measureRight 4 avgt 15 55926.913 542.441 ns/op
o.s.IterationBenchmark.measureRight 5 avgt 15 557258.750 22156.055 ns/op
o.s.IterationBenchmark.measureRight 6 avgt 15 5726578.719 54460.453 ns/op
o.s.IterationBenchmark.measureWrong 1 avgt 15 20.705 0.075 ns/op
o.s.IterationBenchmark.measureWrong 2 avgt 15 125.606 0.388 ns/op
o.s.IterationBenchmark.measureWrong 3 avgt 15 1162.701 8.058 ns/op
o.s.IterationBenchmark.measureWrong 4 avgt 15 11759.684 96.262 ns/op
o.s.IterationBenchmark.measureWrong 5 avgt 15 146168.024 3460.825 ns/op
o.s.IterationBenchmark.measureWrong 6 avgt 15 2201680.817 21412.890 ns/op
@kisileno
Copy link

Is there code elimination or it is not so obvious?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment