Skip to content

Instantly share code, notes, and snippets.

@apangin
Created September 6, 2020 20:06
Show Gist options
  • Save apangin/3d86fe03a89bd01693717b2f3c3fb592 to your computer and use it in GitHub Desktop.
Save apangin/3d86fe03a89bd01693717b2f3c3fb592 to your computer and use it in GitHub Desktop.
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());
}
}
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