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