Last active
August 10, 2022 10:29
-
-
Save Randgalt/a68ceee62cd8127431cbe6e7afbfdf44 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 jdk19; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.RunnerException; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.ThreadLocalRandom; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import java.util.stream.IntStream; | |
public class TestEnhancedSwitch | |
{ | |
public sealed interface Fruit | |
{ | |
record Apple() implements Fruit {} | |
record Peach() implements Fruit {} | |
record Pear() implements Fruit {} | |
record Grapes() implements Fruit {} | |
record Cherries() implements Fruit {} | |
} | |
public static void main(String[] args) | |
throws RunnerException | |
{ | |
Options opt = new OptionsBuilder() | |
.include(TestEnhancedSwitch.class.getSimpleName()) | |
.threads(4) | |
.forks(1) | |
.jvmArgs("--enable-preview") | |
.build(); | |
new Runner(opt).run(); | |
} | |
@State(Scope.Benchmark) | |
public static class Data | |
{ | |
public final List<Fruit> fruits; | |
public final Map<Class<?>, AtomicInteger> counts = new ConcurrentHashMap<>(); | |
public Data() | |
{ | |
ThreadLocalRandom random = ThreadLocalRandom.current(); | |
int qty = random.nextInt(1000, 2000); | |
Class<?>[] permittedSubclasses = Fruit.class.getPermittedSubclasses(); | |
fruits = IntStream.range(0, qty) | |
.mapToObj(__ -> { | |
Class<?> clazz = permittedSubclasses[random.nextInt(permittedSubclasses.length)]; | |
counts.computeIfAbsent(clazz, dummy -> new AtomicInteger()); | |
try { | |
return (Fruit) clazz.getConstructor().newInstance(); | |
} | |
catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
}) | |
.toList(); | |
} | |
} | |
@Benchmark | |
public void testEnhancedSwitch(Data data) | |
{ | |
for (Fruit fruit : data.fruits) { | |
switch (fruit) { | |
case Fruit.Apple __ -> data.counts.get(Fruit.Apple.class).incrementAndGet(); | |
case Fruit.Cherries __ -> data.counts.get(Fruit.Cherries.class).incrementAndGet(); | |
case Fruit.Grapes __ -> data.counts.get(Fruit.Grapes.class).incrementAndGet(); | |
case Fruit.Peach __ -> data.counts.get(Fruit.Peach.class).incrementAndGet(); | |
case Fruit.Pear __ -> data.counts.get(Fruit.Pear.class).incrementAndGet(); | |
} | |
} | |
} | |
@Benchmark | |
public void testManualSwitch(Data data) | |
{ | |
for (Fruit fruit : data.fruits) { | |
if (fruit.getClass().equals(Fruit.Apple.class)) { | |
data.counts.get(Fruit.Apple.class).incrementAndGet(); | |
} | |
else if (fruit.getClass().equals(Fruit.Cherries.class)) { | |
data.counts.get(Fruit.Cherries.class).incrementAndGet(); | |
} | |
else if (fruit.getClass().equals(Fruit.Grapes.class)) { | |
data.counts.get(Fruit.Grapes.class).incrementAndGet(); | |
} | |
else if (fruit.getClass().equals(Fruit.Peach.class)) { | |
data.counts.get(Fruit.Peach.class).incrementAndGet(); | |
} | |
else if (fruit.getClass().equals(Fruit.Pear.class)) { | |
data.counts.get(Fruit.Pear.class).incrementAndGet(); | |
} | |
} | |
} | |
} |
Author
Randgalt
commented
Aug 10, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment