Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 0% Scenario{vm=java, trial=0, benchmark=FlipOne} 12.23 ns; ?=0.12 ns @ 6 trials
- * 50% Scenario{vm=java, trial=0, benchmark=FlipTwo} 18.89 ns; ?=0.43 ns @ 10 trials
- * benchmark ns linear runtime
- * FlipOne 12.2 ===================
- * FlipTwo 18.9 ==============================
- * vm: java
- * trial: 0
- */
- import java.util.Random;
- import com.google.caliper.Runner;
- import com.google.caliper.SimpleBenchmark;
- public class FlipTest {
- public static enum FlipOne {
- A, B, Z, Y;
- static {
- A.opposite = Z;
- B.opposite = Y;
- Y.opposite = B;
- Z.opposite = A;
- }
- public FlipOne flip() {
- return opposite;
- }
- private FlipOne opposite;
- }
- public static enum FlipTwo {
- A ("Z"), B ("Y"), Y ("B"), Z ("A");
- private final String opposite;
- private FlipTwo(String opposite) {
- this.opposite = opposite;
- }
- public FlipTwo flip() {
- return valueOf(opposite);
- }
- }
- public static class TestBenchmark extends SimpleBenchmark {
- private Random r;
- private int[] counters;
- @Override
- protected void setUp() throws Exception {
- r = new Random();
- counters = new int[4];
- }
- public int timeFlipOne(int reps) {
- for (int i = 0; i < reps; i++) {
- FlipOne randomFlip = FlipOne.values()[r.nextInt(4)];
- counters[randomFlip.flip().ordinal()]++;
- }
- return counters[0] * counters[1] * counters[2] * counters[3];
- }
- public int timeFlipTwo(int reps) {
- for (int i = 0; i < reps; i++) {
- FlipTwo randomFlip = FlipTwo.values()[r.nextInt(4)];
- counters[randomFlip.flip().ordinal()]++;
- }
- return counters[0] * counters[1] * counters[2] * counters[3];
- }
- }
- public static void main(String[] args) {
- Runner.main(TestBenchmark.class, new String[0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement