package jdk.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.Setup; import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.Warmup; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Map; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @State(Scope.Thread) @Fork(1) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 7) @Measurement(iterations = 10) public class UnmodifiableMapOfBench { Integer i1, i2, i3, i4, i5, i6, i7, i8, i9, i10; @SuppressWarnings("deprecation") @Setup public void setup() { Iterator ii = ThreadLocalRandom.current() .ints(20) .mapToObj(Integer::new) .collect(Collectors.toCollection(LinkedHashSet::new)) .iterator(); i1 = ii.next(); i2 = ii.next(); i3 = ii.next(); i4 = ii.next(); i5 = ii.next(); i6 = ii.next(); i7 = ii.next(); i8 = ii.next(); i9 = ii.next(); i10 = ii.next(); } @Benchmark public Map of_2() { return Map.of(i1, i1, i2, i2); } @Benchmark public Map of_3() { return Map.of(i1, i1, i2, i2, i3, i3); } @Benchmark public Map of_4() { return Map.of(i1, i1, i2, i2, i3, i3, i4, i4); } @Benchmark public Map of_5() { return Map.of(i1, i1, i2, i2, i3, i3, i4, i4, i5, i5); } @Benchmark public Map of_6() { return Map.of(i1, i1, i2, i2, i3, i3, i4, i4, i5, i5, i6, i6); } @Benchmark public Map of_7() { return Map.of(i1, i1, i2, i2, i3, i3, i4, i4, i5, i5, i6, i6, i7, i7); } @Benchmark public Map of_8() { return Map.of(i1, i1, i2, i2, i3, i3, i4, i4, i5, i5, i6, i6, i7, i7, i8, i8); } @Benchmark public Map of_9() { return Map.of(i1, i1, i2, i2, i3, i3, i4, i4, i5, i5, i6, i6, i7, i7, i8, i8, i9, i9); } @Benchmark public Map of10() { return Map.of(i1, i1, i2, i2, i3, i3, i4, i4, i5, i5, i6, i6, i7, i7, i8, i8, i9, i9, i10, i10); } }