Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import org.openjdk.jmh.annotations.Benchmark;
- import org.openjdk.jmh.annotations.BenchmarkMode;
- 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.infra.Blackhole;
- import java.util.Random;
- import java.util.concurrent.TimeUnit;
- import static io.vavr.API.$;
- import static io.vavr.API.Case;
- import static io.vavr.API.Match;
- import static test.MyBenchmark.TestEnum.A;
- import static test.MyBenchmark.TestEnum.B;
- import static test.MyBenchmark.TestEnum.C;
- import static test.MyBenchmark.TestEnum.values;
- @BenchmarkMode(Mode.AverageTime)
- @OutputTimeUnit(TimeUnit.MICROSECONDS)
- public class MyBenchmark {
- public enum TestEnum {A, B, C}
- @State(Scope.Thread)
- public static class MyState {
- public Random r = new Random();
- }
- @Benchmark
- public void ifMethod(Blackhole bh, MyState state) {
- TestEnum sample = TestEnum.values()[state.r.nextInt(values().length)];
- TestEnum result;
- if (sample == A) {
- result = B;
- } else if (sample == B) {
- result = C;
- } else if (sample == C) {
- result = A;
- } else {
- result = A;
- }
- bh.consume(result);
- }
- @Benchmark
- public void switchMethod(Blackhole bh, MyState state) {
- TestEnum sample = TestEnum.values()[state.r.nextInt(values().length)];
- TestEnum result;
- switch (sample) {
- case A:
- result = B;
- case B:
- result = C;
- case C:
- result = A;
- default:
- result = A;
- }
- bh.consume(result);
- }
- @Benchmark
- public void vavrMethod(Blackhole bh, MyState state) {
- TestEnum sample = TestEnum.values()[state.r.nextInt(values().length)];
- bh.consume(Match(sample).of(
- Case($(A), B),
- Case($(B), C),
- Case($(C), A),
- Case($(), A)
- ));
- }
- public static void main(String[] args) throws Exception {
- org.openjdk.jmh.Main.main(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement