-
-
Save plevart/3ddada53824faede66e843acd0dfa92f to your computer and use it in GitHub Desktop.
ClassValue vs. Constant benchmark
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
/* | |
JDK14: | |
Benchmark Mode Cnt Score Error Units | |
ClassValueBench.testConstantString avgt 10 2.737 ± 0.008 ns/op | |
ClassValueBench.testInteger avgt 10 4.332 ± 0.020 ns/op | |
ClassValueBench.testObject avgt 10 4.433 ± 0.017 ns/op | |
ClassValueBench.testString avgt 10 4.328 ± 0.016 ns/op | |
patched valhalla: | |
Benchmark Mode Cnt Score Error Units | |
ClassValueBench.testConstantString avgt 10 2.683 ± 0.010 ns/op | |
ClassValueBench.testInteger avgt 10 2.680 ± 0.006 ns/op | |
ClassValueBench.testObject avgt 10 2.684 ± 0.005 ns/op | |
ClassValueBench.testString avgt 10 2.682 ± 0.006 ns/op | |
*/ | |
package my.jmh.test; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Measurement; | |
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.annotations.Warmup; | |
import java.util.concurrent.TimeUnit; | |
@BenchmarkMode(Mode.AverageTime) | |
@Warmup(iterations = 5, time = 1) | |
@Measurement(iterations = 10, time = 1) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@State(Scope.Benchmark) | |
@Fork(value = 1, warmups = 0) | |
public class ClassValueBench { | |
static final ClassValue<String> CV = new ClassValue<>() { | |
@Override | |
protected String computeValue(Class<?> type) { | |
return type.getName(); | |
} | |
}; | |
@Benchmark | |
public String testConstantString() { | |
return "A constant"; | |
} | |
@Benchmark | |
public String testString() { | |
return CV.get(String.class); | |
} | |
@Benchmark | |
public String testInteger() { | |
return CV.get(Integer.class); | |
} | |
@Benchmark | |
public String testObject() { | |
return CV.get(Object.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment