author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 45937 | jdk/test/java/util/Collection/IteratorMicroBenchmark.java@646816090183 |
child 47307 | 6864969a78ad |
permissions | -rw-r--r-- |
42319 | 1 |
/* |
2 |
* Copyright (c) 2007, 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 |
/* |
|
25 |
* @test |
|
26 |
* @summary micro-benchmark correctness mode |
|
27 |
* @run main IteratorMicroBenchmark iterations=1 size=8 warmup=0 |
|
28 |
*/ |
|
29 |
||
30 |
import static java.util.stream.Collectors.summingInt; |
|
31 |
||
32 |
import java.lang.ref.WeakReference; |
|
33 |
import java.util.ArrayDeque; |
|
34 |
import java.util.Arrays; |
|
35 |
import java.util.ArrayList; |
|
36 |
import java.util.Collection; |
|
37 |
import java.util.Collections; |
|
38 |
import java.util.Deque; |
|
39 |
import java.util.Enumeration; |
|
40 |
import java.util.Iterator; |
|
41 |
import java.util.LinkedList; |
|
42 |
import java.util.List; |
|
43 |
import java.util.ListIterator; |
|
44 |
import java.util.Map; |
|
45 |
import java.util.PriorityQueue; |
|
46 |
import java.util.Spliterator; |
|
47 |
import java.util.Vector; |
|
48 |
import java.util.concurrent.ArrayBlockingQueue; |
|
49 |
import java.util.concurrent.ConcurrentLinkedDeque; |
|
50 |
import java.util.concurrent.ConcurrentLinkedQueue; |
|
51 |
import java.util.concurrent.LinkedBlockingDeque; |
|
52 |
import java.util.concurrent.LinkedBlockingQueue; |
|
53 |
import java.util.concurrent.LinkedTransferQueue; |
|
54 |
import java.util.concurrent.PriorityBlockingQueue; |
|
55 |
import java.util.concurrent.ConcurrentSkipListMap; |
|
56 |
import java.util.concurrent.CountDownLatch; |
|
57 |
import java.util.concurrent.ThreadLocalRandom; |
|
58 |
import java.util.concurrent.TimeUnit; |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
59 |
import java.util.concurrent.atomic.LongAdder; |
42319 | 60 |
import java.util.regex.Pattern; |
61 |
||
62 |
/** |
|
63 |
* Usage: [iterations=N] [size=N] [filter=REGEXP] [warmup=SECONDS] |
|
64 |
* |
|
65 |
* To run this in micro-benchmark mode, simply run as a normal java program. |
|
66 |
* Be patient; this program runs for a very long time. |
|
67 |
* For faster runs, restrict execution using command line args. |
|
68 |
* |
|
69 |
* This is an interface based version of ArrayList/IteratorMicroBenchmark |
|
70 |
* |
|
71 |
* @author Martin Buchholz |
|
72 |
*/ |
|
73 |
public class IteratorMicroBenchmark { |
|
74 |
abstract static class Job { |
|
75 |
private final String name; |
|
76 |
public Job(String name) { this.name = name; } |
|
77 |
public String name() { return name; } |
|
78 |
public abstract void work() throws Throwable; |
|
79 |
} |
|
80 |
||
81 |
final int iterations; |
|
82 |
final int size; // number of elements in collections |
|
83 |
final double warmupSeconds; |
|
84 |
final long warmupNanos; |
|
85 |
final Pattern filter; // select subset of Jobs to run |
|
86 |
final boolean reverse; // reverse order of Jobs |
|
87 |
final boolean shuffle; // randomize order of Jobs |
|
88 |
||
89 |
IteratorMicroBenchmark(String[] args) { |
|
90 |
iterations = intArg(args, "iterations", 10_000); |
|
91 |
size = intArg(args, "size", 1000); |
|
92 |
warmupSeconds = doubleArg(args, "warmup", 7.0); |
|
93 |
filter = patternArg(args, "filter"); |
|
94 |
reverse = booleanArg(args, "reverse"); |
|
95 |
shuffle = booleanArg(args, "shuffle"); |
|
96 |
||
97 |
warmupNanos = (long) (warmupSeconds * (1000L * 1000L * 1000L)); |
|
98 |
} |
|
99 |
||
100 |
// --------------- GC finalization infrastructure --------------- |
|
101 |
||
102 |
/** No guarantees, but effective in practice. */ |
|
103 |
static void forceFullGc() { |
|
104 |
CountDownLatch finalizeDone = new CountDownLatch(1); |
|
105 |
WeakReference<?> ref = new WeakReference<Object>(new Object() { |
|
106 |
protected void finalize() { finalizeDone.countDown(); }}); |
|
107 |
try { |
|
108 |
for (int i = 0; i < 10; i++) { |
|
109 |
System.gc(); |
|
110 |
if (finalizeDone.await(1L, TimeUnit.SECONDS) && ref.get() == null) { |
|
111 |
System.runFinalization(); // try to pick up stragglers |
|
112 |
return; |
|
113 |
} |
|
114 |
} |
|
115 |
} catch (InterruptedException unexpected) { |
|
116 |
throw new AssertionError("unexpected InterruptedException"); |
|
117 |
} |
|
118 |
throw new AssertionError("failed to do a \"full\" gc"); |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Runs each job for long enough that all the runtime compilers |
|
123 |
* have had plenty of time to warm up, i.e. get around to |
|
124 |
* compiling everything worth compiling. |
|
125 |
* Returns array of average times per job per run. |
|
126 |
*/ |
|
127 |
long[] time0(List<Job> jobs) throws Throwable { |
|
128 |
final int size = jobs.size(); |
|
129 |
long[] nanoss = new long[size]; |
|
130 |
for (int i = 0; i < size; i++) { |
|
131 |
if (warmupNanos > 0) forceFullGc(); |
|
132 |
Job job = jobs.get(i); |
|
133 |
long totalTime; |
|
134 |
int runs = 0; |
|
135 |
long startTime = System.nanoTime(); |
|
136 |
do { job.work(); runs++; } |
|
137 |
while ((totalTime = System.nanoTime() - startTime) < warmupNanos); |
|
138 |
nanoss[i] = totalTime/runs; |
|
139 |
} |
|
140 |
return nanoss; |
|
141 |
} |
|
142 |
||
143 |
void time(List<Job> jobs) throws Throwable { |
|
144 |
if (warmupNanos > 0) time0(jobs); // Warm up run |
|
145 |
final int size = jobs.size(); |
|
146 |
final long[] nanoss = time0(jobs); // Real timing run |
|
147 |
final long[] milliss = new long[size]; |
|
148 |
final double[] ratios = new double[size]; |
|
149 |
||
150 |
final String nameHeader = "Method"; |
|
151 |
final String millisHeader = "Millis"; |
|
152 |
final String ratioHeader = "Ratio"; |
|
153 |
||
154 |
int nameWidth = nameHeader.length(); |
|
155 |
int millisWidth = millisHeader.length(); |
|
156 |
int ratioWidth = ratioHeader.length(); |
|
157 |
||
158 |
for (int i = 0; i < size; i++) { |
|
159 |
nameWidth = Math.max(nameWidth, jobs.get(i).name().length()); |
|
160 |
||
161 |
milliss[i] = nanoss[i]/(1000L * 1000L); |
|
162 |
millisWidth = Math.max(millisWidth, |
|
163 |
String.format("%d", milliss[i]).length()); |
|
164 |
||
165 |
ratios[i] = (double) nanoss[i] / (double) nanoss[0]; |
|
166 |
ratioWidth = Math.max(ratioWidth, |
|
167 |
String.format("%.3f", ratios[i]).length()); |
|
168 |
} |
|
169 |
||
170 |
String format = String.format("%%-%ds %%%dd %%%d.3f%%n", |
|
171 |
nameWidth, millisWidth, ratioWidth); |
|
172 |
String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n", |
|
173 |
nameWidth, millisWidth, ratioWidth); |
|
174 |
System.out.printf(headerFormat, "Method", "Millis", "Ratio"); |
|
175 |
||
176 |
// Print out absolute and relative times, calibrated against first job |
|
177 |
for (int i = 0; i < size; i++) |
|
178 |
System.out.printf(format, jobs.get(i).name(), milliss[i], ratios[i]); |
|
179 |
} |
|
180 |
||
181 |
private static String keywordValue(String[] args, String keyword) { |
|
182 |
for (String arg : args) |
|
183 |
if (arg.startsWith(keyword)) |
|
184 |
return arg.substring(keyword.length() + 1); |
|
185 |
return null; |
|
186 |
} |
|
187 |
||
188 |
private static int intArg(String[] args, String keyword, int defaultValue) { |
|
189 |
String val = keywordValue(args, keyword); |
|
190 |
return (val == null) ? defaultValue : Integer.parseInt(val); |
|
191 |
} |
|
192 |
||
193 |
private static double doubleArg(String[] args, String keyword, double defaultValue) { |
|
194 |
String val = keywordValue(args, keyword); |
|
195 |
return (val == null) ? defaultValue : Double.parseDouble(val); |
|
196 |
} |
|
197 |
||
198 |
private static Pattern patternArg(String[] args, String keyword) { |
|
199 |
String val = keywordValue(args, keyword); |
|
200 |
return (val == null) ? null : Pattern.compile(val); |
|
201 |
} |
|
202 |
||
203 |
private static boolean booleanArg(String[] args, String keyword) { |
|
204 |
String val = keywordValue(args, keyword); |
|
205 |
if (val == null || val.equals("false")) return false; |
|
206 |
if (val.equals("true")) return true; |
|
207 |
throw new IllegalArgumentException(val); |
|
208 |
} |
|
209 |
||
210 |
private static List<Job> filter(Pattern filter, List<Job> jobs) { |
|
211 |
if (filter == null) return jobs; |
|
212 |
ArrayList<Job> newJobs = new ArrayList<>(); |
|
213 |
for (Job job : jobs) |
|
214 |
if (filter.matcher(job.name()).find()) |
|
215 |
newJobs.add(job); |
|
216 |
return newJobs; |
|
217 |
} |
|
218 |
||
219 |
private static void deoptimize(int sum) { |
|
220 |
if (sum == 42) |
|
221 |
System.out.println("the answer"); |
|
222 |
} |
|
223 |
||
224 |
private static <T> List<T> asSubList(List<T> list) { |
|
225 |
return list.subList(0, list.size()); |
|
226 |
} |
|
227 |
||
228 |
private static <T> Iterable<T> backwards(final List<T> list) { |
|
229 |
return new Iterable<T>() { |
|
230 |
public Iterator<T> iterator() { |
|
231 |
return new Iterator<T>() { |
|
232 |
final ListIterator<T> it = list.listIterator(list.size()); |
|
233 |
public boolean hasNext() { return it.hasPrevious(); } |
|
234 |
public T next() { return it.previous(); } |
|
235 |
public void remove() { it.remove(); }};}}; |
|
236 |
} |
|
237 |
||
238 |
// Checks for correctness *and* prevents loop optimizations |
|
239 |
class Check { |
|
240 |
private int sum; |
|
241 |
public void sum(int sum) { |
|
242 |
if (this.sum == 0) |
|
243 |
this.sum = sum; |
|
244 |
if (this.sum != sum) |
|
245 |
throw new AssertionError("Sum mismatch"); |
|
246 |
} |
|
247 |
} |
|
248 |
volatile Check check = new Check(); |
|
249 |
||
250 |
public static void main(String[] args) throws Throwable { |
|
251 |
new IteratorMicroBenchmark(args).run(); |
|
252 |
} |
|
253 |
||
254 |
void run() throws Throwable { |
|
255 |
// System.out.printf( |
|
256 |
// "iterations=%d size=%d, warmup=%1g, filter=\"%s\"%n", |
|
257 |
// iterations, size, warmupSeconds, filter); |
|
258 |
||
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
259 |
final ArrayList<Integer> al = new ArrayList<>(size); |
42319 | 260 |
|
261 |
// Populate collections with random data |
|
262 |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
|
263 |
for (int i = 0; i < size; i++) |
|
264 |
al.add(rnd.nextInt(size)); |
|
265 |
||
266 |
final ArrayDeque<Integer> ad = new ArrayDeque<>(al); |
|
267 |
final ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(al.size()); |
|
268 |
abq.addAll(al); |
|
269 |
||
270 |
// shuffle circular array elements so they wrap |
|
271 |
for (int i = 0, n = rnd.nextInt(size); i < n; i++) { |
|
272 |
ad.addLast(ad.removeFirst()); |
|
273 |
abq.add(abq.remove()); |
|
274 |
} |
|
275 |
||
276 |
ArrayList<Job> jobs = new ArrayList<>(Arrays.asList()); |
|
277 |
||
278 |
List.of(al, ad, abq, |
|
279 |
new LinkedList<>(al), |
|
280 |
new PriorityQueue<>(al), |
|
281 |
new Vector<>(al), |
|
282 |
new ConcurrentLinkedQueue<>(al), |
|
283 |
new ConcurrentLinkedDeque<>(al), |
|
284 |
new LinkedBlockingQueue<>(al), |
|
285 |
new LinkedBlockingDeque<>(al), |
|
286 |
new LinkedTransferQueue<>(al), |
|
287 |
new PriorityBlockingQueue<>(al)) |
|
288 |
.stream() |
|
289 |
.forEach(x -> { |
|
290 |
jobs.addAll(collectionJobs(x)); |
|
291 |
if (x instanceof Deque) |
|
292 |
jobs.addAll(dequeJobs((Deque<Integer>)x)); |
|
293 |
}); |
|
294 |
||
295 |
if (reverse) Collections.reverse(jobs); |
|
296 |
if (shuffle) Collections.shuffle(jobs); |
|
297 |
||
298 |
time(filter(filter, jobs)); |
|
299 |
} |
|
300 |
||
301 |
List<Job> collectionJobs(Collection<Integer> x) { |
|
302 |
String klazz = x.getClass().getSimpleName(); |
|
303 |
return List.of( |
|
304 |
new Job(klazz + " iterate for loop") { |
|
305 |
public void work() throws Throwable { |
|
306 |
for (int i = 0; i < iterations; i++) { |
|
307 |
int sum = 0; |
|
308 |
for (Integer n : x) |
|
309 |
sum += n; |
|
310 |
check.sum(sum);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
311 |
new Job(klazz + " iterator().forEachRemaining()") { |
42319 | 312 |
public void work() throws Throwable { |
313 |
int[] sum = new int[1]; |
|
314 |
for (int i = 0; i < iterations; i++) { |
|
315 |
sum[0] = 0; |
|
316 |
x.iterator().forEachRemaining(n -> sum[0] += n); |
|
317 |
check.sum(sum[0]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
318 |
new Job(klazz + " spliterator().tryAdvance()") { |
42319 | 319 |
public void work() throws Throwable { |
320 |
int[] sum = new int[1]; |
|
321 |
for (int i = 0; i < iterations; i++) { |
|
322 |
sum[0] = 0; |
|
323 |
Spliterator<Integer> spliterator = x.spliterator(); |
|
324 |
do {} while (spliterator.tryAdvance(n -> sum[0] += n)); |
|
325 |
check.sum(sum[0]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
326 |
new Job(klazz + " spliterator().forEachRemaining()") { |
42319 | 327 |
public void work() throws Throwable { |
328 |
int[] sum = new int[1]; |
|
329 |
for (int i = 0; i < iterations; i++) { |
|
330 |
sum[0] = 0; |
|
331 |
x.spliterator().forEachRemaining(n -> sum[0] += n); |
|
332 |
check.sum(sum[0]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
333 |
new Job(klazz + " removeIf") { |
42319 | 334 |
public void work() throws Throwable { |
335 |
int[] sum = new int[1]; |
|
336 |
for (int i = 0; i < iterations; i++) { |
|
337 |
sum[0] = 0; |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
338 |
if (x.removeIf(n -> { sum[0] += n; return false; })) |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
339 |
throw new AssertionError(); |
42319 | 340 |
check.sum(sum[0]);}}}, |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
341 |
new Job(klazz + " contains") { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
342 |
public void work() throws Throwable { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
343 |
int[] sum = new int[1]; |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
344 |
Object y = new Object() { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
345 |
public boolean equals(Object z) { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
346 |
sum[0] += (int) z; return false; }}; |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
347 |
for (int i = 0; i < iterations; i++) { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
348 |
sum[0] = 0; |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
349 |
if (x.contains(y)) throw new AssertionError(); |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
350 |
check.sum(sum[0]);}}}, |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
351 |
new Job(klazz + " remove(Object)") { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
352 |
public void work() throws Throwable { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
353 |
int[] sum = new int[1]; |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
354 |
Object y = new Object() { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
355 |
public boolean equals(Object z) { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
356 |
sum[0] += (int) z; return false; }}; |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
357 |
for (int i = 0; i < iterations; i++) { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
358 |
sum[0] = 0; |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
359 |
if (x.remove(y)) throw new AssertionError(); |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
360 |
check.sum(sum[0]);}}}, |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
361 |
new Job(klazz + " forEach") { |
42319 | 362 |
public void work() throws Throwable { |
363 |
int[] sum = new int[1]; |
|
364 |
for (int i = 0; i < iterations; i++) { |
|
365 |
sum[0] = 0; |
|
366 |
x.forEach(n -> sum[0] += n); |
|
367 |
check.sum(sum[0]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
368 |
new Job(klazz + " toArray()") { |
42319 | 369 |
public void work() throws Throwable { |
370 |
int[] sum = new int[1]; |
|
371 |
for (int i = 0; i < iterations; i++) { |
|
372 |
sum[0] = 0; |
|
373 |
for (Object o : x.toArray()) |
|
374 |
sum[0] += (Integer) o; |
|
375 |
check.sum(sum[0]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
376 |
new Job(klazz + " toArray(a)") { |
42319 | 377 |
public void work() throws Throwable { |
378 |
Integer[] a = new Integer[x.size()]; |
|
379 |
int[] sum = new int[1]; |
|
380 |
for (int i = 0; i < iterations; i++) { |
|
381 |
sum[0] = 0; |
|
382 |
x.toArray(a); |
|
383 |
for (Object o : a) |
|
384 |
sum[0] += (Integer) o; |
|
385 |
check.sum(sum[0]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
386 |
new Job(klazz + " toArray(empty)") { |
42319 | 387 |
public void work() throws Throwable { |
388 |
Integer[] empty = new Integer[0]; |
|
389 |
int[] sum = new int[1]; |
|
390 |
for (int i = 0; i < iterations; i++) { |
|
391 |
sum[0] = 0; |
|
392 |
for (Integer o : x.toArray(empty)) |
|
393 |
sum[0] += o; |
|
394 |
check.sum(sum[0]);}}}, |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
395 |
new Job(klazz + " stream().forEach") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
396 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
397 |
int[] sum = new int[1]; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
398 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
399 |
sum[0] = 0; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
400 |
x.stream().forEach(n -> sum[0] += n); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
401 |
check.sum(sum[0]);}}}, |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
402 |
new Job(klazz + " stream().mapToInt") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
403 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
404 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
405 |
check.sum(x.stream().mapToInt(e -> e).sum());}}}, |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
406 |
new Job(klazz + " stream().collect") { |
42319 | 407 |
public void work() throws Throwable { |
408 |
for (int i = 0; i < iterations; i++) { |
|
409 |
check.sum(x.stream() |
|
410 |
.collect(summingInt(e -> e)));}}}, |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
411 |
new Job(klazz + " stream()::iterator") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
412 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
413 |
int[] sum = new int[1]; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
414 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
415 |
sum[0] = 0; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
416 |
for (Integer o : (Iterable<Integer>) x.stream()::iterator) |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
417 |
sum[0] += o; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
418 |
check.sum(sum[0]);}}}, |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
419 |
new Job(klazz + " parallelStream().forEach") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
420 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
421 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
422 |
LongAdder sum = new LongAdder(); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
423 |
x.parallelStream().forEach(n -> sum.add(n)); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
424 |
check.sum((int) sum.sum());}}}, |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
425 |
new Job(klazz + " parallelStream().mapToInt") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
426 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
427 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
428 |
check.sum(x.parallelStream().mapToInt(e -> e).sum());}}}, |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
429 |
new Job(klazz + " parallelStream().collect") { |
42319 | 430 |
public void work() throws Throwable { |
431 |
for (int i = 0; i < iterations; i++) { |
|
432 |
check.sum(x.parallelStream() |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
433 |
.collect(summingInt(e -> e)));}}}, |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
434 |
new Job(klazz + " parallelStream()::iterator") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
435 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
436 |
int[] sum = new int[1]; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
437 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
438 |
sum[0] = 0; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
439 |
for (Integer o : (Iterable<Integer>) x.parallelStream()::iterator) |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
440 |
sum[0] += o; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
441 |
check.sum(sum[0]);}}}); |
42319 | 442 |
} |
443 |
||
444 |
List<Job> dequeJobs(Deque<Integer> x) { |
|
445 |
String klazz = x.getClass().getSimpleName(); |
|
446 |
return List.of( |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
447 |
new Job(klazz + " descendingIterator() loop") { |
42319 | 448 |
public void work() throws Throwable { |
449 |
for (int i = 0; i < iterations; i++) { |
|
450 |
int sum = 0; |
|
451 |
Iterator<Integer> it = x.descendingIterator(); |
|
452 |
while (it.hasNext()) |
|
453 |
sum += it.next(); |
|
454 |
check.sum(sum);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
455 |
new Job(klazz + " descendingIterator().forEachRemaining()") { |
42319 | 456 |
public void work() throws Throwable { |
457 |
int[] sum = new int[1]; |
|
458 |
for (int i = 0; i < iterations; i++) { |
|
459 |
sum[0] = 0; |
|
460 |
x.descendingIterator().forEachRemaining(n -> sum[0] += n); |
|
461 |
check.sum(sum[0]);}}}); |
|
462 |
} |
|
463 |
} |