Skip to content

Instantly share code, notes, and snippets.

@amaembo
Last active May 31, 2018 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amaembo/cbe9e1a8f3f8d240beb080343670f0c2 to your computer and use it in GitHub Desktop.
Save amaembo/cbe9e1a8f3f8d240beb080343670f0c2 to your computer and use it in GitHub Desktop.
# JMH 1.13 (released 139 days ago)
# VM version: JDK 1.8.0_101, VM 25.101-b13
# VM invoker: C:\Program Files\Java\jdk1.8.0_101\jre\bin\java.exe
# VM options: <none>
Benchmark Mode Cnt Score Error Units
MapTest.fancy avgt 15 42821,529 ± 425,169 ns/op
MapTest.oldSchool avgt 15 182,887 ± 2,666 ns/op
import org.openjdk.jmh.annotations.*;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(3)
@State(Scope.Benchmark)
public class MapTest {
public interface KeyValueStringFunction<T> extends Function<String, T>, Serializable {
default String key() {
return functionalMethod().getParameters()[0].getName();
}
default T value() {
return apply(key());
}
default Method functionalMethod() {
final SerializedLambda serialzedLabmda = serializedLambda();
final Class<?> implementationClass = implementationClass(serialzedLabmda);
return Stream.of(implementationClass.getDeclaredMethods())
.filter(m -> Objects.equals(m.getName(), serialzedLabmda.getImplMethodName()))
.findFirst()
.orElseThrow(RuntimeException::new);
}
default Class<?> implementationClass(SerializedLambda serializedLambda) {
try {
final String className = serializedLambda.getImplClass().replaceAll("/", ".");
return Class.forName(className);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
default SerializedLambda serializedLambda() {
try {
final Method replaceMethod = getClass().getDeclaredMethod("writeReplace");
replaceMethod.setAccessible(true);
return (SerializedLambda) replaceMethod.invoke(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@SafeVarargs
static <V> Map<String, V> mapOf(KeyValueStringFunction<V>... mappings) {
return Stream.of(mappings)
.collect(
Collectors.toMap(
KeyValueStringFunction::key,
KeyValueStringFunction::value
)
);
}
}
public static class Maps {
public static <K, V> Map<K, V> of(K k1, V v1) {
return Collections.singletonMap(k1, v1);
}
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2) {
Map<K, V> map = new HashMap<>();
map.put(k1, v1);
map.put(k2, v2);
return map;
}
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
Map<K, V> map = new HashMap<>();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
return map;
}
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
Map<K, V> map = new HashMap<>();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
return map;
}
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
Map<K, V> map = new HashMap<>();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
map.put(k5, v5);
return map;
}
// continue if you're like
}
@Benchmark
public Map<String, Map<String, Integer>> fancy() {
return KeyValueStringFunction.mapOf(
usa -> KeyValueStringFunction.mapOf(
new_york -> 8_550_405,
los_angeles -> 3_971_883,
chicago -> 2_720_546
),
canada -> KeyValueStringFunction.mapOf(
toronto -> 2_615_060,
montreal -> 1_649_519,
calgary -> 1_096_833
)
);
}
@Benchmark
public Map<String, Map<String, Integer>> oldSchool() {
return Maps.of(
"usa" , Maps.of(
"new_york" , 8_550_405,
"los_angeles" , 3_971_883,
"chicago" , 2_720_546
),
"canada" , Maps.of(
"toronto" , 2_615_060,
"montreal" , 1_649_519,
"calgary" , 1_096_833
)
);
}
}
# JMH 1.13 (released 139 days ago)
# VM version: JDK 1.8.0_101, VM 25.101-b13
# VM invoker: C:\Program Files\Java\jdk1.8.0_101\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 500 ms each
# Measurement: 5 iterations, 500 ms each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.example.MapTest.fancy
# Run progress: 0,00% complete, ETA 00:00:30
# Fork: 1 of 3
# Warmup Iteration 1: 64180,734 ns/op
# Warmup Iteration 2: 43214,874 ns/op
# Warmup Iteration 3: 46478,840 ns/op
# Warmup Iteration 4: 42429,870 ns/op
# Warmup Iteration 5: 43279,656 ns/op
Iteration 1: 42796,956 ns/op
Iteration 2: 42534,985 ns/op
Iteration 3: 42400,713 ns/op
Iteration 4: 42592,634 ns/op
Iteration 5: 42338,768 ns/op
# Run progress: 16,67% complete, ETA 00:00:26
# Fork: 2 of 3
# Warmup Iteration 1: 65591,349 ns/op
# Warmup Iteration 2: 43281,099 ns/op
# Warmup Iteration 3: 43494,893 ns/op
# Warmup Iteration 4: 43583,397 ns/op
# Warmup Iteration 5: 42796,050 ns/op
Iteration 1: 43168,731 ns/op
Iteration 2: 43542,718 ns/op
Iteration 3: 42949,553 ns/op
Iteration 4: 42573,148 ns/op
Iteration 5: 42439,348 ns/op
# Run progress: 33,33% complete, ETA 00:00:21
# Fork: 3 of 3
# Warmup Iteration 1: 65814,014 ns/op
# Warmup Iteration 2: 43806,589 ns/op
# Warmup Iteration 3: 47327,194 ns/op
# Warmup Iteration 4: 43106,877 ns/op
# Warmup Iteration 5: 43630,530 ns/op
Iteration 1: 43299,582 ns/op
Iteration 2: 42397,365 ns/op
Iteration 3: 43432,398 ns/op
Iteration 4: 42926,815 ns/op
Iteration 5: 42929,221 ns/op
Result "fancy":
42821,529 ±(99.9%) 425,169 ns/op [Average]
(min, avg, max) = (42338,768, 42821,529, 43542,718), stdev = 397,703
CI (99.9%): [42396,360, 43246,698] (assumes normal distribution)
# JMH 1.13 (released 139 days ago)
# VM version: JDK 1.8.0_101, VM 25.101-b13
# VM invoker: C:\Program Files\Java\jdk1.8.0_101\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 500 ms each
# Measurement: 5 iterations, 500 ms each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.example.MapTest.oldSchool
# Run progress: 50,00% complete, ETA 00:00:15
# Fork: 1 of 3
# Warmup Iteration 1: 249,428 ns/op
# Warmup Iteration 2: 243,986 ns/op
# Warmup Iteration 3: 209,563 ns/op
# Warmup Iteration 4: 284,515 ns/op
# Warmup Iteration 5: 179,470 ns/op
Iteration 1: 186,048 ns/op
Iteration 2: 183,430 ns/op
Iteration 3: 182,803 ns/op
Iteration 4: 183,313 ns/op
Iteration 5: 183,533 ns/op
# Run progress: 66,67% complete, ETA 00:00:10
# Fork: 2 of 3
# Warmup Iteration 1: 251,519 ns/op
# Warmup Iteration 2: 244,423 ns/op
# Warmup Iteration 3: 212,975 ns/op
# Warmup Iteration 4: 275,071 ns/op
# Warmup Iteration 5: 183,705 ns/op
Iteration 1: 183,017 ns/op
Iteration 2: 183,891 ns/op
Iteration 3: 184,690 ns/op
Iteration 4: 188,091 ns/op
Iteration 5: 181,658 ns/op
# Run progress: 83,33% complete, ETA 00:00:05
# Fork: 3 of 3
# Warmup Iteration 1: 250,888 ns/op
# Warmup Iteration 2: 240,091 ns/op
# Warmup Iteration 3: 206,969 ns/op
# Warmup Iteration 4: 277,986 ns/op
# Warmup Iteration 5: 182,737 ns/op
Iteration 1: 182,185 ns/op
Iteration 2: 181,019 ns/op
Iteration 3: 180,086 ns/op
Iteration 4: 177,318 ns/op
Iteration 5: 182,222 ns/op
Result "oldSchool":
182,887 ±(99.9%) 2,666 ns/op [Average]
(min, avg, max) = (177,318, 182,887, 188,091), stdev = 2,494
CI (99.9%): [180,221, 185,553] (assumes normal distribution)
# Run complete. Total time: 00:00:32
Benchmark Mode Cnt Score Error Units
MapTest.fancy avgt 15 42821,529 ± 425,169 ns/op
MapTest.oldSchool avgt 15 182,887 ± 2,666 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment