Skip to content

Instantly share code, notes, and snippets.

@shipilev
Created March 5, 2014 06:07
Show Gist options
  • Save shipilev/9362043 to your computer and use it in GitHub Desktop.
Save shipilev/9362043 to your computer and use it in GitHub Desktop.
Dead code is so dead
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);
}
}
/*
* This is a wrong way to measure: one could reasonably expect
* dead code elimination. It does not happen though because we
* have the effect from the implicit class check exception
* against Integer.
*/
@GenerateMicroBenchmark
public void measureWrong_Integer() {
for (Integer i : list) {}
}
/*
* ...which is fragile, and breaks if we do this:
*/
@GenerateMicroBenchmark
public void measureWrong_Object() {
for (Object i : list) {}
}
/*
* This is a right way to do this: provide the explicit side
* effect on polled value.
*/
@GenerateMicroBenchmark
public void measureRight_Integer(BlackHole bh) {
for (Integer i : list) {
bh.consume(i);
}
}
/*
* ...changing to Object contrasts the class check costs:
*/
@GenerateMicroBenchmark
public void measureRight_Object(BlackHole bh) {
for (Object i : list) {
bh.consume(i);
}
}
}
Linux, x86_64, JDK 7u40:
Benchmark (sizeOrder) Mode Samples Mean Mean error Units
o.s.IterationBenchmark.measureRight_Integer 1 avgt 15 50.813 1.817 ns/op
o.s.IterationBenchmark.measureRight_Integer 2 avgt 15 502.060 19.911 ns/op
o.s.IterationBenchmark.measureRight_Integer 3 avgt 15 4709.723 70.036 ns/op
o.s.IterationBenchmark.measureRight_Integer 4 avgt 15 55548.441 144.832 ns/op
o.s.IterationBenchmark.measureRight_Integer 5 avgt 15 552038.716 20420.720 ns/op
o.s.IterationBenchmark.measureRight_Integer 6 avgt 15 5689806.172 36583.892 ns/op
o.s.IterationBenchmark.measureRight_Object 1 avgt 15 49.056 0.467 ns/op
o.s.IterationBenchmark.measureRight_Object 2 avgt 15 479.963 6.473 ns/op
o.s.IterationBenchmark.measureRight_Object 3 avgt 15 4814.110 166.472 ns/op
o.s.IterationBenchmark.measureRight_Object 4 avgt 15 50196.706 357.950 ns/op
o.s.IterationBenchmark.measureRight_Object 5 avgt 15 495062.244 21404.860 ns/op
o.s.IterationBenchmark.measureRight_Object 6 avgt 15 4558569.261 29702.827 ns/op
o.s.IterationBenchmark.measureWrong_Integer 1 avgt 15 20.701 0.114 ns/op
o.s.IterationBenchmark.measureWrong_Integer 2 avgt 15 126.029 1.050 ns/op
o.s.IterationBenchmark.measureWrong_Integer 3 avgt 15 1160.442 5.388 ns/op
o.s.IterationBenchmark.measureWrong_Integer 4 avgt 15 11568.539 105.846 ns/op
o.s.IterationBenchmark.measureWrong_Integer 5 avgt 15 141681.315 2374.405 ns/op
o.s.IterationBenchmark.measureWrong_Integer 6 avgt 15 2134986.761 11975.542 ns/op
o.s.IterationBenchmark.measureWrong_Object 1 avgt 15 11.050 0.054 ns/op
o.s.IterationBenchmark.measureWrong_Object 2 avgt 15 11.071 0.071 ns/op
o.s.IterationBenchmark.measureWrong_Object 3 avgt 15 11.047 0.016 ns/op
o.s.IterationBenchmark.measureWrong_Object 4 avgt 15 11.192 0.239 ns/op
o.s.IterationBenchmark.measureWrong_Object 5 avgt 15 11.477 0.496 ns/op
o.s.IterationBenchmark.measureWrong_Object 6 avgt 15 12.875 2.178 ns/op
@vlsi
Copy link

vlsi commented Mar 5, 2014

Any chance you add indexed loop?

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