Skip to content

Instantly share code, notes, and snippets.

@doom369
Created September 7, 2020 07:40
Show Gist options
  • Save doom369/8c5777731ed7c815bee5433bc0e0fe15 to your computer and use it in GitHub Desktop.
Save doom369/8c5777731ed7c815bee5433bc0e0fe15 to your computer and use it in GitHub Desktop.
@BenchmarkMode(Mode.AverageTime)
@Fork(5)
@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 init() {
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());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment