Created
September 6, 2020 20:06
-
-
Save apangin/3d86fe03a89bd01693717b2f3c3fb592 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
package string; | |
import org.openjdk.jmh.annotations.*; | |
import java.util.Random; | |
import java.util.concurrent.TimeUnit; | |
@BenchmarkMode(Mode.AverageTime) | |
@Fork(1) | |
@State(Scope.Thread) | |
@Warmup(iterations = 5, time = 1) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@Measurement(iterations = 10, time = 1) | |
public class EnumValueOf { | |
String[] names; | |
int index; | |
@Setup | |
public void setup() { | |
WidgetProperty[] values = WidgetProperty.values(); | |
names = new Random(0).ints(4096, 0, values.length) | |
.mapToObj(i -> values[i].name()) | |
.toArray(String[]::new); | |
index = 0; | |
} | |
private String nextName() { | |
return names[index++ & (names.length - 1)]; | |
} | |
@Benchmark | |
public WidgetProperty enumValueOf() { | |
return WidgetProperty.valueOf(nextName()); | |
} | |
@Benchmark | |
public WidgetProperty enumSwitch() { | |
return WidgetProperty.valueOfSwitch(nextName()); | |
} | |
@Benchmark | |
public WidgetProperty enumMap() { | |
return WidgetProperty.valueOfMap(nextName()); | |
} | |
} |
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
Benchmark Mode Cnt Score Error Units | |
EnumValueOf.enumMap avgt 10 7,023 ± 0,037 ns/op | |
EnumValueOf.enumSwitch avgt 10 21,160 ± 0,161 ns/op | |
EnumValueOf.enumValueOf avgt 10 6,137 ± 0,071 ns/op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment