Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DateTimeFormatter benchmark
package info.unportant.gist.jsr310;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.YEAR;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
public class DTFBenchmark {
private static final DateTimeFormatter withoutOptformatter =
new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendValue(YEAR, 4)
.appendValue(MONTH_OF_YEAR, 2)
.appendValue(DAY_OF_MONTH, 2)
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT);
private static final DateTimeFormatter patternFormatter =
DateTimeFormatter.ofPattern("yyyyMMdd").withResolverStyle(ResolverStyle.STRICT);
@Param(value = {"0", "32", "128"})
private int stackDepth;
@Param(value = {"1", "100"})
private int batchSize;
LocalDate now = LocalDate.now();
@Benchmark
public LocalDate baseline() {
return recursive(stackDepth, () -> now);
}
@Benchmark
public String basicIsoDate() {
return recursive(
stackDepth,
() -> now.format(DateTimeFormatter.BASIC_ISO_DATE));
}
@Benchmark
public String withoutOpt() {
return recursive(stackDepth, () -> now.format(withoutOptformatter));
}
@Benchmark
public String ofPattern() {
return recursive(stackDepth, () -> now.format(patternFormatter));
}
private <T> T recursive(int depth, Supplier<T> supplier) {
if (depth == 0) {
T t = null;
for (int i = 0; i < batchSize; i++) {
t = supplier.get();
}
return t;
}
return recursive(depth - 1, supplier);
}
}
mh {
jmhVersion = 1.14
include = '.*DateTimeFormatter.*'
warmupForks = 0
fork = 2
warmup = '1s'
warmupIterations = 3
timeOnIteration = '1s'
iterations = 6
failOnError = true
forceGC = true
resultsFile = project.file("${project.buildDir}/reports/jmh/results.txt")
resultFormat = 'JSON'
}
Benchmark (batchSize) (stackDepth) Mode Cnt Score Error Units
DTFBenchmark.baseline 1 0 avgt 12 0.004 ± 0.001 us/op
DTFBenchmark.baseline 1 32 avgt 12 0.036 ± 0.001 us/op
DTFBenchmark.baseline 1 128 avgt 12 0.156 ± 0.008 us/op
DTFBenchmark.baseline 100 0 avgt 12 0.004 ± 0.001 us/op
DTFBenchmark.baseline 100 32 avgt 12 0.037 ± 0.001 us/op
DTFBenchmark.baseline 100 128 avgt 12 0.128 ± 0.002 us/op
DTFBenchmark.basicIsoDate 1 0 avgt 12 2.525 ± 0.242 us/op
DTFBenchmark.basicIsoDate 1 32 avgt 12 4.893 ± 0.148 us/op
DTFBenchmark.basicIsoDate 1 128 avgt 12 11.512 ± 0.230 us/op
DTFBenchmark.basicIsoDate 100 0 avgt 12 252.686 ± 22.203 us/op
DTFBenchmark.basicIsoDate 100 32 avgt 12 469.255 ± 9.449 us/op
DTFBenchmark.basicIsoDate 100 128 avgt 12 1097.172 ± 28.851 us/op
DTFBenchmark.ofPattern 1 0 avgt 12 0.125 ± 0.003 us/op
DTFBenchmark.ofPattern 1 32 avgt 12 0.176 ± 0.003 us/op
DTFBenchmark.ofPattern 1 128 avgt 12 0.325 ± 0.006 us/op
DTFBenchmark.ofPattern 100 0 avgt 12 11.874 ± 0.273 us/op
DTFBenchmark.ofPattern 100 32 avgt 12 11.734 ± 0.405 us/op
DTFBenchmark.ofPattern 100 128 avgt 12 11.740 ± 0.347 us/op
DTFBenchmark.withoutOpt 1 0 avgt 12 0.124 ± 0.004 us/op
DTFBenchmark.withoutOpt 1 32 avgt 12 0.174 ± 0.005 us/op
DTFBenchmark.withoutOpt 1 128 avgt 12 0.327 ± 0.005 us/op
DTFBenchmark.withoutOpt 100 0 avgt 12 11.891 ± 0.247 us/op
DTFBenchmark.withoutOpt 100 32 avgt 12 11.347 ± 0.221 us/op
DTFBenchmark.withoutOpt 100 128 avgt 12 12.536 ± 1.670 us/op
$ export JAVA_HOME=~/Sources/openjdk/jdk9/archived_builds/new/
$ ./gradlew jmh
Benchmark (batchSize) (stackDepth) Mode Cnt Score Error Units
DTFBenchmark.baseline 1 0 avgt 12 0.004 ± 0.001 us/op
DTFBenchmark.baseline 1 32 avgt 12 0.036 ± 0.001 us/op
DTFBenchmark.baseline 1 128 avgt 12 0.152 ± 0.007 us/op
DTFBenchmark.baseline 100 0 avgt 12 0.004 ± 0.001 us/op
DTFBenchmark.baseline 100 32 avgt 12 0.036 ± 0.002 us/op
DTFBenchmark.baseline 100 128 avgt 12 0.129 ± 0.002 us/op
DTFBenchmark.basicIsoDate 1 0 avgt 12 0.176 ± 0.009 us/op
DTFBenchmark.basicIsoDate 1 32 avgt 12 0.229 ± 0.007 us/op
DTFBenchmark.basicIsoDate 1 128 avgt 12 0.388 ± 0.013 us/op
DTFBenchmark.basicIsoDate 100 0 avgt 12 16.415 ± 0.310 us/op
DTFBenchmark.basicIsoDate 100 32 avgt 12 18.011 ± 0.997 us/op
DTFBenchmark.basicIsoDate 100 128 avgt 12 16.767 ± 0.376 us/op
DTFBenchmark.ofPattern 1 0 avgt 12 0.122 ± 0.001 us/op
DTFBenchmark.ofPattern 1 32 avgt 12 0.175 ± 0.002 us/op
DTFBenchmark.ofPattern 1 128 avgt 12 0.334 ± 0.006 us/op
DTFBenchmark.ofPattern 100 0 avgt 12 11.398 ± 0.256 us/op
DTFBenchmark.ofPattern 100 32 avgt 12 11.285 ± 0.243 us/op
DTFBenchmark.ofPattern 100 128 avgt 12 11.572 ± 0.370 us/op
DTFBenchmark.withoutOpt 1 0 avgt 12 0.124 ± 0.003 us/op
DTFBenchmark.withoutOpt 1 32 avgt 12 0.181 ± 0.004 us/op
DTFBenchmark.withoutOpt 1 128 avgt 12 0.330 ± 0.007 us/op
DTFBenchmark.withoutOpt 100 0 avgt 12 11.860 ± 0.359 us/op
DTFBenchmark.withoutOpt 100 32 avgt 12 11.630 ± 0.401 us/op
DTFBenchmark.withoutOpt 100 128 avgt 12 11.749 ± 0.259 us/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment