Skip to content

Commit f736767

Browse files
committedOct 26, 2023
Add Fibonacci examples and benchmarks
1 parent 69f970f commit f736767

File tree

2 files changed

+275
-2
lines changed

2 files changed

+275
-2
lines changed
 

‎src/java.base/share/classes/java/lang/snippet-files/ComputedConstantSnippets.java

+73-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
*/
2525
package java.lang.snippet;
2626

27+
import java.util.HashMap;
2728
import java.util.List;
2829
import java.util.Locale;
30+
import java.util.Map;
2931
import java.util.Optional;
3032
import java.util.ResourceBundle;
33+
import java.util.concurrent.ConcurrentHashMap;
3134
import java.util.function.Supplier;
35+
import java.util.stream.IntStream;
3236

3337
/**
3438
* Snippets for the ComputedConstant class
@@ -139,7 +143,7 @@ private Color calculateBgColor() {
139143
// @end
140144

141145

142-
public static
146+
static
143147

144148
class GermanLabels {
145149

@@ -165,7 +169,7 @@ public static void main(String[] args) {
165169
}
166170

167171

168-
public static
172+
static
169173

170174
class GermanLabels2 {
171175

@@ -198,6 +202,73 @@ public static void main(String[] args) {
198202
}
199203

200204

205+
static
206+
207+
class Fibonacci {
208+
209+
private final Map<Integer, Integer> map;
210+
211+
public Fibonacci(int upperBound) {
212+
map = new ConcurrentHashMap<>(upperBound);
213+
}
214+
215+
public int number(int n) {
216+
return (n < 2)
217+
? n
218+
: map.computeIfAbsent(n, nk -> number(nk - 1) + number(nk - 2) );
219+
}
220+
221+
}
222+
223+
static
224+
225+
class Fibonacci2 {
226+
227+
private final List<ComputedConstant<Integer>> list;
228+
229+
public Fibonacci2(int upperBound) {
230+
list = ComputedConstant.of(upperBound, this::number);
231+
}
232+
233+
public int number(int n) {
234+
return (n < 2)
235+
? n
236+
: list.get(n - 1).get() + list.get(n - 2).get();
237+
}
238+
239+
240+
public static void main(String[] args) {
241+
Fibonacci fibonacci = new Fibonacci(20);
242+
int[] fibs = IntStream.range(0, 10)
243+
.map(fibonacci::number)
244+
.toArray(); // { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }
245+
}
246+
247+
}
248+
249+
static final
250+
251+
class Fibonacci3 {
252+
253+
private static final List<ComputedConstant<Integer>> LIST
254+
= ComputedConstant.of(40, Fibonacci3::number);
255+
256+
public static int number(int n) {
257+
return (n < 2)
258+
? n
259+
: LIST.get(n - 1).get() + LIST.get(n - 2).get();
260+
}
261+
262+
public static void main(String[] args) {
263+
int[] fibs = IntStream.range(0, 10)
264+
.map(Fibonacci3::number)
265+
.toArray(); // { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }
266+
}
267+
268+
}
269+
270+
271+
201272
// Dummy classes
202273
static final class Foo {}
203274
static final class Color {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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

Comments
 (0)
Please sign in to comment.