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