|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +package org.openjdk.bench.java.lang; |
| 25 | + |
| 26 | +import org.openjdk.jmh.annotations.*; |
| 27 | +import org.openjdk.jmh.infra.Blackhole; |
| 28 | + |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.concurrent.ConcurrentHashMap; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +@BenchmarkMode(Mode.AverageTime) |
| 36 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 37 | +@State(Scope.Benchmark) |
| 38 | +@Warmup(iterations = 5, time = 1) |
| 39 | +@Measurement(iterations = 5, time = 1) |
| 40 | +@Fork(value=3, jvmArgsAppend = "--enable-preview") |
| 41 | +public class ComputedConstantMapVsCC { |
| 42 | + |
| 43 | + private static final FibonacciArray fibonacciArray = new FibonacciArray(20); |
| 44 | + private static final FibonacciHashMap fibonacciHashMap = new FibonacciHashMap(20); |
| 45 | + private static final FibonacciConcurrentMap fibonacciConcurrentMap = new FibonacciConcurrentMap(20); |
| 46 | + private static final FibonacciCC fibonacciCC = new FibonacciCC(20); |
| 47 | + private static final FibonacciRecord fibonacciRecord = new FibonacciRecord(20); |
| 48 | + |
| 49 | + @Benchmark |
| 50 | + public void array(Blackhole bh) { |
| 51 | + bh.consume(fibonacciArray.number(10)); |
| 52 | + } |
| 53 | + |
| 54 | + @Benchmark |
| 55 | + public void hashMap(Blackhole bh) { |
| 56 | + bh.consume(fibonacciHashMap.number(10)); |
| 57 | + } |
| 58 | + |
| 59 | + @Benchmark |
| 60 | + public void concurrentMap(Blackhole bh) { |
| 61 | + bh.consume(fibonacciConcurrentMap.number(10)); |
| 62 | + } |
| 63 | + |
| 64 | + @Benchmark |
| 65 | + public void ccUntrusted(Blackhole bh) { |
| 66 | + bh.consume(fibonacciCC.number(10)); |
| 67 | + } |
| 68 | + |
| 69 | + @Fork(value = 3, jvmArgsAppend = {"--enable-preview", "-XX:+UnlockExperimentalVMOptions", "-XX:+TrustFinalNonStaticFields"}) |
| 70 | + @Benchmark |
| 71 | + public void ccTrusted(Blackhole bh) { |
| 72 | + bh.consume(fibonacciCC.number(10)); |
| 73 | + } |
| 74 | + |
| 75 | + @Benchmark |
| 76 | + public void ccRecord(Blackhole bh) { |
| 77 | + bh.consume(fibonacciRecord.number(10)); |
| 78 | + } |
| 79 | + |
| 80 | + static class FibonacciHashMap { |
| 81 | + |
| 82 | + private final Map<Integer, Integer> map; |
| 83 | + |
| 84 | + public FibonacciHashMap(int upperBound) { |
| 85 | + map = new HashMap<>(upperBound); |
| 86 | + } |
| 87 | + |
| 88 | + public int number(int n) { |
| 89 | + if (n < 2) { |
| 90 | + return n; |
| 91 | + } |
| 92 | + Integer v = map.get(n); |
| 93 | + if (v != null) { |
| 94 | + return v; |
| 95 | + } |
| 96 | + int n1 = number(n - 1); |
| 97 | + int n2 = number(n - 2); |
| 98 | + int sum = n1 + n2; |
| 99 | + map.put(n, sum); |
| 100 | + return sum; |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + static class FibonacciConcurrentMap { |
| 106 | + |
| 107 | + private final Map<Integer, Integer> map; |
| 108 | + |
| 109 | + public FibonacciConcurrentMap(int upperBound) { |
| 110 | + map = new ConcurrentHashMap<>(upperBound); |
| 111 | + } |
| 112 | + |
| 113 | + public int number(int n) { |
| 114 | + if (n < 2) { |
| 115 | + return n; |
| 116 | + } |
| 117 | + Integer v = map.get(n); |
| 118 | + if (v != null) { |
| 119 | + return v; |
| 120 | + } |
| 121 | + int n1 = number(n - 1); |
| 122 | + int n2 = number(n - 2); |
| 123 | + int sum = n1 + n2; |
| 124 | + map.put(n, sum); |
| 125 | + return sum; |
| 126 | + } |
| 127 | + |
| 128 | +/* public int number(int n) { |
| 129 | + return (n < 2) |
| 130 | + ? n |
| 131 | + : map.computeIfAbsent(n, nk -> number(nk - 1) + number(nk - 2)); |
| 132 | + }*/ |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + static class FibonacciCC { |
| 137 | + |
| 138 | + private final List<ComputedConstant<Integer>> list; |
| 139 | + |
| 140 | + public FibonacciCC(int upperBound) { |
| 141 | + list = ComputedConstant.of(upperBound, this::number); |
| 142 | + } |
| 143 | + |
| 144 | + public int number(int n) { |
| 145 | + return (n < 2) |
| 146 | + ? n |
| 147 | + : list.get(n - 1).get() + list.get(n - 2).get(); |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + static class FibonacciArray { |
| 153 | + |
| 154 | + private final int[] array; |
| 155 | + |
| 156 | + public FibonacciArray(int upperBound) { |
| 157 | + array = new int[upperBound]; |
| 158 | + } |
| 159 | + |
| 160 | + public int number(int n) { |
| 161 | + if (n < 2) { |
| 162 | + return n; |
| 163 | + } |
| 164 | + int v = array[n]; |
| 165 | + if (v != 0) { |
| 166 | + return v; |
| 167 | + } |
| 168 | + int n1 = number(n - 1); |
| 169 | + int n2 = number(n - 2); |
| 170 | + int sum = n1 + n2; |
| 171 | + array[n] = sum; |
| 172 | + return sum; |
| 173 | + } |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | + record FibonacciRecord(List<ComputedConstant<Integer>> list) { |
| 178 | + |
| 179 | + public FibonacciRecord(int upperBound) { |
| 180 | + this(ComputedConstant.of(upperBound, FibonacciRecord::fib)); |
| 181 | + } |
| 182 | + |
| 183 | + // This will not use the cached values. |
| 184 | + private static int fib(int n) { |
| 185 | + if (n < 2) { |
| 186 | + return n; |
| 187 | + } |
| 188 | + int n1 = fib(n - 1); |
| 189 | + int n2 = fib(n - 2); |
| 190 | + int sum = n1 + n2; |
| 191 | + return sum; |
| 192 | + } |
| 193 | + |
| 194 | + public int number(int n) { |
| 195 | + return (n < 2) |
| 196 | + ? n |
| 197 | + : list.get(n - 1).get() + list.get(n - 2).get(); |
| 198 | + } |
| 199 | + |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments