-
-
Save zivce/bbe743087f1da53e01912d15c15c2a46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Slightly modified benchmark from Optimize Java book | |
package de.jawb.jmh.benchmark.example.uniquechars; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Measurement; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.Warmup; | |
import org.openjdk.jmh.infra.Blackhole; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
@Warmup(iterations = 15, time = 2, timeUnit = TimeUnit.MILLISECONDS) | |
@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.MILLISECONDS) | |
@Fork(1) | |
@State(Scope.Benchmark) | |
@OutputTimeUnit(TimeUnit.MILLISECONDS) | |
@BenchmarkMode({Mode.Throughput}) | |
public class BimorphicBenchmark { | |
private interface Shape { | |
int getSides(); | |
} | |
private class Triangle implements Shape { | |
public int getSides() { | |
return 3; | |
} | |
} | |
private class Square implements Shape { | |
public int getSides() { | |
return 4; | |
} | |
} | |
private class Octagon implements Shape { | |
public int getSides() { | |
return 8; | |
} | |
} | |
private java.util.Random random = new java.util.Random(); | |
private Shape triangle = new Triangle(); | |
private Shape square = new Square(); | |
private Shape octagon = new Octagon(); | |
@Benchmark | |
public int runMonomorphic() { | |
Shape currentShape = triangle; | |
return currentShape.getSides(); | |
} | |
@Benchmark | |
public int runBimorphic() { | |
Shape currentShape = null; | |
switch (random.nextInt(2)) | |
{ | |
case 0: | |
currentShape = triangle; | |
break; | |
case 1: | |
currentShape = square; | |
break; | |
} | |
return currentShape.getSides(); | |
} | |
@Benchmark | |
public int runMegamorphic() { | |
Shape currentShape = null; | |
switch (random.nextInt(3)) | |
{ | |
case 0: | |
currentShape = triangle; | |
break; | |
case 1: | |
currentShape = square; | |
break; | |
case 2: | |
currentShape = octagon; | |
break; | |
} | |
return currentShape.getSides(); | |
} | |
@Benchmark | |
public int runPeeledMegamorphic() { | |
Shape currentShape = null; | |
switch (random.nextInt(3)) | |
{ | |
case 0: | |
currentShape = triangle; | |
break; | |
case 1: | |
currentShape = square; | |
break; | |
case 2: | |
currentShape = octagon; | |
break; | |
} | |
// peel one observed type from the original call site | |
if (currentShape instanceof Triangle) { | |
return ((Triangle) currentShape).getSides(); | |
} | |
else { | |
return currentShape.getSides(); // now only bimorphic | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment