1 |
jsr166 |
1.1 |
/* |
2 |
jsr166 |
1.2 |
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. |
3 |
jsr166 |
1.1 |
* 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 |
jsr166 |
1.2 |
* 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 |
jsr166 |
1.1 |
*/ |
23 |
|
|
|
24 |
|
|
/* |
25 |
jsr166 |
1.3 |
* @test |
26 |
|
|
* @summary micro-benchmark correctness mode |
27 |
|
|
* @run main IteratorMicroBenchmark iterations=1 size=8 warmup=0 |
28 |
|
|
*/ |
29 |
|
|
|
30 |
jsr166 |
1.17 |
import static java.util.stream.Collectors.toList; |
31 |
|
|
|
32 |
jsr166 |
1.3 |
import java.lang.ref.WeakReference; |
33 |
|
|
import java.util.ArrayDeque; |
34 |
|
|
import java.util.ArrayList; |
35 |
jsr166 |
1.17 |
import java.util.Arrays; |
36 |
jsr166 |
1.3 |
import java.util.Enumeration; |
37 |
|
|
import java.util.Iterator; |
38 |
|
|
import java.util.List; |
39 |
|
|
import java.util.ListIterator; |
40 |
|
|
import java.util.Map; |
41 |
jsr166 |
1.8 |
import java.util.Spliterator; |
42 |
jsr166 |
1.3 |
import java.util.Vector; |
43 |
|
|
import java.util.concurrent.ConcurrentSkipListMap; |
44 |
|
|
import java.util.concurrent.CountDownLatch; |
45 |
|
|
import java.util.concurrent.ThreadLocalRandom; |
46 |
|
|
import java.util.concurrent.TimeUnit; |
47 |
|
|
import java.util.regex.Pattern; |
48 |
|
|
|
49 |
|
|
/** |
50 |
|
|
* Usage: [iterations=N] [size=N] [filter=REGEXP] [warmup=SECONDS] |
51 |
jsr166 |
1.1 |
* |
52 |
jsr166 |
1.3 |
* To run this in micro-benchmark mode, simply run as a normal java program. |
53 |
jsr166 |
1.13 |
* Be patient; this program runs for a very long time. |
54 |
jsr166 |
1.3 |
* For faster runs, restrict execution using command line args. |
55 |
jsr166 |
1.1 |
* |
56 |
|
|
* @author Martin Buchholz |
57 |
|
|
*/ |
58 |
|
|
public class IteratorMicroBenchmark { |
59 |
|
|
abstract static class Job { |
60 |
|
|
private final String name; |
61 |
|
|
public Job(String name) { this.name = name; } |
62 |
|
|
public String name() { return name; } |
63 |
|
|
public abstract void work() throws Throwable; |
64 |
|
|
} |
65 |
|
|
|
66 |
jsr166 |
1.15 |
static double warmupSeconds; |
67 |
|
|
static long warmupNanos; |
68 |
jsr166 |
1.3 |
|
69 |
|
|
// --------------- GC finalization infrastructure --------------- |
70 |
|
|
|
71 |
|
|
/** No guarantees, but effective in practice. */ |
72 |
|
|
static void forceFullGc() { |
73 |
|
|
CountDownLatch finalizeDone = new CountDownLatch(1); |
74 |
|
|
WeakReference<?> ref = new WeakReference<Object>(new Object() { |
75 |
|
|
protected void finalize() { finalizeDone.countDown(); }}); |
76 |
jsr166 |
1.1 |
try { |
77 |
jsr166 |
1.3 |
for (int i = 0; i < 10; i++) { |
78 |
|
|
System.gc(); |
79 |
|
|
if (finalizeDone.await(1L, TimeUnit.SECONDS) && ref.get() == null) { |
80 |
|
|
System.runFinalization(); // try to pick up stragglers |
81 |
|
|
return; |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
} catch (InterruptedException unexpected) { |
85 |
|
|
throw new AssertionError("unexpected InterruptedException"); |
86 |
|
|
} |
87 |
|
|
throw new AssertionError("failed to do a \"full\" gc"); |
88 |
jsr166 |
1.1 |
} |
89 |
|
|
|
90 |
|
|
/** |
91 |
|
|
* Runs each job for long enough that all the runtime compilers |
92 |
|
|
* have had plenty of time to warm up, i.e. get around to |
93 |
|
|
* compiling everything worth compiling. |
94 |
|
|
* Returns array of average times per job per run. |
95 |
|
|
*/ |
96 |
jsr166 |
1.11 |
private static long[] time0(Job ... jobs) throws Throwable { |
97 |
|
|
long[] nanoss = new long[jobs.length]; |
98 |
|
|
for (int i = 0; i < jobs.length; i++) { |
99 |
jsr166 |
1.3 |
if (warmupNanos > 0) forceFullGc(); |
100 |
jsr166 |
1.15 |
Job job = jobs[i]; |
101 |
|
|
long totalTime; |
102 |
|
|
int runs = 0; |
103 |
|
|
long startTime = System.nanoTime(); |
104 |
|
|
do { job.work(); runs++; } |
105 |
|
|
while ((totalTime = System.nanoTime() - startTime) < warmupNanos); |
106 |
|
|
nanoss[i] = totalTime/runs; |
107 |
jsr166 |
1.1 |
} |
108 |
|
|
return nanoss; |
109 |
|
|
} |
110 |
|
|
|
111 |
jsr166 |
1.11 |
private static void time(Job ... jobs) throws Throwable { |
112 |
jsr166 |
1.15 |
if (warmupSeconds > 0.0) time0(jobs); // Warm up run |
113 |
jsr166 |
1.11 |
long[] nanoss = time0(jobs); // Real timing run |
114 |
|
|
long[] milliss = new long[jobs.length]; |
115 |
|
|
double[] ratios = new double[jobs.length]; |
116 |
jsr166 |
1.1 |
|
117 |
|
|
final String nameHeader = "Method"; |
118 |
|
|
final String millisHeader = "Millis"; |
119 |
|
|
final String ratioHeader = "Ratio"; |
120 |
|
|
|
121 |
|
|
int nameWidth = nameHeader.length(); |
122 |
|
|
int millisWidth = millisHeader.length(); |
123 |
|
|
int ratioWidth = ratioHeader.length(); |
124 |
|
|
|
125 |
jsr166 |
1.11 |
for (int i = 0; i < jobs.length; i++) { |
126 |
|
|
nameWidth = Math.max(nameWidth, jobs[i].name().length()); |
127 |
jsr166 |
1.1 |
|
128 |
|
|
milliss[i] = nanoss[i]/(1000L * 1000L); |
129 |
|
|
millisWidth = Math.max(millisWidth, |
130 |
|
|
String.format("%d", milliss[i]).length()); |
131 |
|
|
|
132 |
|
|
ratios[i] = (double) nanoss[i] / (double) nanoss[0]; |
133 |
|
|
ratioWidth = Math.max(ratioWidth, |
134 |
|
|
String.format("%.3f", ratios[i]).length()); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
String format = String.format("%%-%ds %%%dd %%%d.3f%%n", |
138 |
|
|
nameWidth, millisWidth, ratioWidth); |
139 |
|
|
String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n", |
140 |
|
|
nameWidth, millisWidth, ratioWidth); |
141 |
|
|
System.out.printf(headerFormat, "Method", "Millis", "Ratio"); |
142 |
|
|
|
143 |
|
|
// Print out absolute and relative times, calibrated against first job |
144 |
jsr166 |
1.11 |
for (int i = 0; i < jobs.length; i++) |
145 |
|
|
System.out.printf(format, jobs[i].name(), milliss[i], ratios[i]); |
146 |
jsr166 |
1.1 |
} |
147 |
|
|
|
148 |
|
|
private static String keywordValue(String[] args, String keyword) { |
149 |
|
|
for (String arg : args) |
150 |
|
|
if (arg.startsWith(keyword)) |
151 |
|
|
return arg.substring(keyword.length() + 1); |
152 |
|
|
return null; |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
private static int intArg(String[] args, String keyword, int defaultValue) { |
156 |
|
|
String val = keywordValue(args, keyword); |
157 |
jsr166 |
1.3 |
return (val == null) ? defaultValue : Integer.parseInt(val); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
private static double doubleArg(String[] args, String keyword, double defaultValue) { |
161 |
|
|
String val = keywordValue(args, keyword); |
162 |
|
|
return (val == null) ? defaultValue : Double.parseDouble(val); |
163 |
jsr166 |
1.1 |
} |
164 |
|
|
|
165 |
|
|
private static Pattern patternArg(String[] args, String keyword) { |
166 |
|
|
String val = keywordValue(args, keyword); |
167 |
jsr166 |
1.3 |
return (val == null) ? null : Pattern.compile(val); |
168 |
jsr166 |
1.1 |
} |
169 |
|
|
|
170 |
jsr166 |
1.11 |
private static Job[] filter(Pattern filter, Job[] jobs) { |
171 |
jsr166 |
1.17 |
return (filter == null) ? jobs |
172 |
|
|
: Arrays.stream(jobs) |
173 |
|
|
.filter(job -> filter.matcher(job.name()).find()) |
174 |
|
|
.collect(toList()) |
175 |
|
|
.toArray(new Job[0]); |
176 |
jsr166 |
1.1 |
} |
177 |
|
|
|
178 |
|
|
private static void deoptimize(int sum) { |
179 |
|
|
if (sum == 42) |
180 |
|
|
System.out.println("the answer"); |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
private static <T> List<T> asSubList(List<T> list) { |
184 |
|
|
return list.subList(0, list.size()); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
private static <T> Iterable<T> backwards(final List<T> list) { |
188 |
|
|
return new Iterable<T>() { |
189 |
|
|
public Iterator<T> iterator() { |
190 |
|
|
return new Iterator<T>() { |
191 |
|
|
final ListIterator<T> it = list.listIterator(list.size()); |
192 |
|
|
public boolean hasNext() { return it.hasPrevious(); } |
193 |
|
|
public T next() { return it.previous(); } |
194 |
|
|
public void remove() { it.remove(); }};}}; |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
public static void main(String[] args) throws Throwable { |
198 |
jsr166 |
1.11 |
final int iterations = intArg(args, "iterations", 100_000); |
199 |
|
|
final int size = intArg(args, "size", 1000); |
200 |
jsr166 |
1.15 |
warmupSeconds = doubleArg(args, "warmup", 7.0); |
201 |
jsr166 |
1.11 |
final Pattern filter = patternArg(args, "filter"); |
202 |
jsr166 |
1.15 |
|
203 |
|
|
warmupNanos = (long) (warmupSeconds * (1000L * 1000L * 1000L)); |
204 |
|
|
|
205 |
jsr166 |
1.3 |
// System.out.printf( |
206 |
|
|
// "iterations=%d size=%d, warmup=%1g, filter=\"%s\"%n", |
207 |
|
|
// iterations, size, warmupSeconds, filter); |
208 |
jsr166 |
1.1 |
|
209 |
|
|
final ConcurrentSkipListMap<Integer,Integer> m |
210 |
jsr166 |
1.16 |
= new ConcurrentSkipListMap<>(); |
211 |
|
|
final ArrayList<Integer> al = new ArrayList<>(size); |
212 |
jsr166 |
1.1 |
|
213 |
|
|
// Populate collections with random data |
214 |
jsr166 |
1.3 |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
215 |
jsr166 |
1.1 |
for (int i = 0; i < size; i++) { |
216 |
|
|
m.put(rnd.nextInt(size), rnd.nextInt(size)); |
217 |
jsr166 |
1.6 |
al.add(rnd.nextInt(size)); |
218 |
jsr166 |
1.1 |
} |
219 |
jsr166 |
1.16 |
final Vector<Integer> v = new Vector<>(al); |
220 |
|
|
final ArrayDeque<Integer> ad = new ArrayDeque<>(al); |
221 |
jsr166 |
1.7 |
// shuffle ArrayDeque elements so they wrap |
222 |
|
|
for (int i = 0, n = rnd.nextInt(size); i < n; i++) |
223 |
|
|
ad.addLast(ad.removeFirst()); |
224 |
jsr166 |
1.1 |
|
225 |
|
|
// Also test "short" collections |
226 |
|
|
final int shortSize = 5; |
227 |
jsr166 |
1.16 |
final Vector<Integer> sv = new Vector<>(v.subList(0, shortSize)); |
228 |
|
|
final ArrayList<Integer> sal = new ArrayList<>(sv); |
229 |
jsr166 |
1.1 |
|
230 |
jsr166 |
1.11 |
// Checks for correctness *and* prevents loop optimizations |
231 |
|
|
class Check { |
232 |
|
|
private int sum; |
233 |
|
|
public void sum(int sum) { |
234 |
|
|
if (this.sum == 0) |
235 |
|
|
this.sum = sum; |
236 |
|
|
if (this.sum != sum) |
237 |
|
|
throw new AssertionError("Sum mismatch"); |
238 |
|
|
} |
239 |
|
|
} |
240 |
|
|
final Check check = new Check(); |
241 |
|
|
final Check shortCheck = new Check(); |
242 |
|
|
|
243 |
|
|
Job[] jobs = { |
244 |
jsr166 |
1.1 |
// new Job("Vector iterate desugared") { |
245 |
|
|
// public void work() throws Throwable { |
246 |
|
|
// for (int i = 0; i < iterations; i++) { |
247 |
|
|
// int sum = 0; |
248 |
|
|
// for (Iterator<Integer> it = v.iterator(); it.hasNext();) |
249 |
|
|
// sum += it.next(); |
250 |
|
|
// check.sum(sum);}}}, |
251 |
|
|
new Job("array loop") { |
252 |
|
|
public void work() throws Throwable { |
253 |
|
|
Integer[] a = al.toArray(new Integer[0]); |
254 |
|
|
for (int i = 0; i < iterations; i++) { |
255 |
|
|
int sum = 0; |
256 |
|
|
int size = a.length; |
257 |
|
|
for (int j = 0; j < size; ++j) |
258 |
|
|
sum += a[j]; |
259 |
|
|
check.sum(sum);}}}, |
260 |
jsr166 |
1.4 |
new Job("descending array loop") { |
261 |
|
|
public void work() throws Throwable { |
262 |
|
|
Integer[] a = al.toArray(new Integer[0]); |
263 |
|
|
for (int i = 0; i < iterations; i++) { |
264 |
|
|
int sum = 0; |
265 |
|
|
int size = a.length; |
266 |
|
|
for (int j = size - 1; j >= 0; j--) |
267 |
|
|
sum += a[j]; |
268 |
|
|
check.sum(sum);}}}, |
269 |
jsr166 |
1.1 |
new Job("Vector get loop") { |
270 |
|
|
public void work() throws Throwable { |
271 |
|
|
for (int i = 0; i < iterations; i++) { |
272 |
|
|
int sum = 0; |
273 |
|
|
int size = v.size(); |
274 |
|
|
for (int j = 0; j < size; ++j) |
275 |
|
|
sum += v.get(j); |
276 |
|
|
check.sum(sum);}}}, |
277 |
|
|
new Job("Vector iterate for loop") { |
278 |
|
|
public void work() throws Throwable { |
279 |
|
|
for (int i = 0; i < iterations; i++) { |
280 |
|
|
int sum = 0; |
281 |
|
|
for (Integer n : v) |
282 |
|
|
sum += n; |
283 |
|
|
check.sum(sum);}}}, |
284 |
|
|
new Job("Vector descending listIterator loop") { |
285 |
|
|
public void work() throws Throwable { |
286 |
|
|
for (int i = 0; i < iterations; i++) { |
287 |
|
|
int sum = 0; |
288 |
|
|
ListIterator<Integer> it = v.listIterator(al.size()); |
289 |
|
|
while (it.hasPrevious()) |
290 |
|
|
sum += it.previous(); |
291 |
|
|
check.sum(sum);}}}, |
292 |
|
|
new Job("Vector Enumeration loop") { |
293 |
|
|
public void work() throws Throwable { |
294 |
|
|
for (int i = 0; i < iterations; i++) { |
295 |
|
|
int sum = 0; |
296 |
|
|
Enumeration<Integer> it = v.elements(); |
297 |
|
|
while (it.hasMoreElements()) |
298 |
|
|
sum += it.nextElement(); |
299 |
|
|
check.sum(sum);}}}, |
300 |
|
|
new Job("Vector subList iterate for loop") { |
301 |
|
|
public void work() throws Throwable { |
302 |
|
|
for (int i = 0; i < iterations; i++) { |
303 |
|
|
int sum = 0; |
304 |
|
|
for (Integer n : asSubList(v)) |
305 |
|
|
sum += n; |
306 |
|
|
check.sum(sum);}}}, |
307 |
|
|
new Job("Vector subList subList subList iterate for loop") { |
308 |
|
|
public void work() throws Throwable { |
309 |
|
|
for (int i = 0; i < iterations; i++) { |
310 |
|
|
int sum = 0; |
311 |
|
|
for (Integer n : asSubList(asSubList(asSubList(v)))) |
312 |
|
|
sum += n; |
313 |
|
|
check.sum(sum);}}}, |
314 |
|
|
new Job("Vector backwards wrapper ListIterator for loop") { |
315 |
|
|
public void work() throws Throwable { |
316 |
|
|
for (int i = 0; i < iterations; i++) { |
317 |
|
|
int sum = 0; |
318 |
|
|
for (Integer n : backwards(v)) |
319 |
|
|
sum += n; |
320 |
|
|
check.sum(sum);}}}, |
321 |
|
|
new Job("Vector backwards wrapper subList ListIterator for loop") { |
322 |
|
|
public void work() throws Throwable { |
323 |
|
|
for (int i = 0; i < iterations; i++) { |
324 |
|
|
int sum = 0; |
325 |
|
|
for (Integer n : backwards(asSubList(v))) |
326 |
|
|
sum += n; |
327 |
|
|
check.sum(sum);}}}, |
328 |
|
|
// new Job("Vector iterate for loop invokeinterface") { |
329 |
|
|
// public void work() throws Throwable { |
330 |
|
|
// final List<Integer> l = v; |
331 |
|
|
// for (int i = 0; i < iterations; i++) { |
332 |
|
|
// int sum = 0; |
333 |
|
|
// for (Integer n : l) |
334 |
|
|
// sum += n; |
335 |
|
|
// check.sum(sum);}}}, |
336 |
|
|
// new Job("Vector subList iterate for loop invokeinterface") { |
337 |
|
|
// public void work() throws Throwable { |
338 |
|
|
// final List<Integer> l = v; |
339 |
|
|
// for (int i = 0; i < iterations; i++) { |
340 |
|
|
// int sum = 0; |
341 |
|
|
// for (Integer n : asSubList(l)) |
342 |
|
|
// sum += n; |
343 |
|
|
// check.sum(sum);}}}, |
344 |
|
|
new Job("Short Vector get loop") { |
345 |
|
|
public void work() throws Throwable { |
346 |
|
|
for (int i = 0; i < (iterations * size / shortSize); i++) { |
347 |
|
|
int sum = 0; |
348 |
|
|
int size = sv.size(); |
349 |
|
|
for (int j = 0; j < size; ++j) |
350 |
|
|
sum += sv.get(j); |
351 |
|
|
shortCheck.sum(sum);}}}, |
352 |
|
|
new Job("Short Vector iterate for loop") { |
353 |
|
|
public void work() throws Throwable { |
354 |
|
|
for (int i = 0; i < (iterations * size / shortSize); i++) { |
355 |
|
|
int sum = 0; |
356 |
|
|
for (Integer n : sv) |
357 |
|
|
sum += n; |
358 |
|
|
shortCheck.sum(sum);}}}, |
359 |
|
|
new Job("Short Vector sublist iterate for loop") { |
360 |
|
|
public void work() throws Throwable { |
361 |
|
|
for (int i = 0; i < (iterations * size / shortSize); i++) { |
362 |
|
|
int sum = 0; |
363 |
|
|
for (Integer n : asSubList(sv)) |
364 |
|
|
sum += n; |
365 |
|
|
shortCheck.sum(sum);}}}, |
366 |
|
|
new Job("ArrayList get loop") { |
367 |
|
|
public void work() throws Throwable { |
368 |
|
|
for (int i = 0; i < iterations; i++) { |
369 |
|
|
int sum = 0; |
370 |
|
|
int size = al.size(); |
371 |
|
|
for (int j = 0; j < size; ++j) |
372 |
|
|
sum += al.get(j); |
373 |
|
|
check.sum(sum);}}}, |
374 |
|
|
new Job("ArrayList iterate for loop") { |
375 |
|
|
public void work() throws Throwable { |
376 |
|
|
for (int i = 0; i < iterations; i++) { |
377 |
|
|
int sum = 0; |
378 |
|
|
for (Integer n : al) |
379 |
|
|
sum += n; |
380 |
|
|
check.sum(sum);}}}, |
381 |
jsr166 |
1.3 |
new Job("ArrayDeque iterate for loop") { |
382 |
|
|
public void work() throws Throwable { |
383 |
|
|
for (int i = 0; i < iterations; i++) { |
384 |
|
|
int sum = 0; |
385 |
|
|
for (Integer n : ad) |
386 |
|
|
sum += n; |
387 |
|
|
check.sum(sum);}}}, |
388 |
jsr166 |
1.1 |
new Job("ArrayList descending listIterator loop") { |
389 |
|
|
public void work() throws Throwable { |
390 |
|
|
for (int i = 0; i < iterations; i++) { |
391 |
|
|
int sum = 0; |
392 |
|
|
ListIterator<Integer> it = al.listIterator(al.size()); |
393 |
|
|
while (it.hasPrevious()) |
394 |
|
|
sum += it.previous(); |
395 |
|
|
check.sum(sum);}}}, |
396 |
|
|
new Job("ArrayList listIterator loop") { |
397 |
|
|
public void work() throws Throwable { |
398 |
|
|
for (int i = 0; i < iterations; i++) { |
399 |
|
|
int sum = 0; |
400 |
|
|
ListIterator<Integer> it = al.listIterator(); |
401 |
|
|
while (it.hasNext()) |
402 |
|
|
sum += it.next(); |
403 |
|
|
check.sum(sum);}}}, |
404 |
jsr166 |
1.8 |
new Job("ArrayDeque.descendingIterator() loop") { |
405 |
jsr166 |
1.3 |
public void work() throws Throwable { |
406 |
|
|
for (int i = 0; i < iterations; i++) { |
407 |
|
|
int sum = 0; |
408 |
|
|
Iterator<Integer> it = ad.descendingIterator(); |
409 |
|
|
while (it.hasNext()) |
410 |
|
|
sum += it.next(); |
411 |
|
|
check.sum(sum);}}}, |
412 |
|
|
new Job("ArrayList.forEach") { |
413 |
|
|
public void work() throws Throwable { |
414 |
jsr166 |
1.5 |
int[] sum = new int[1]; |
415 |
jsr166 |
1.3 |
for (int i = 0; i < iterations; i++) { |
416 |
jsr166 |
1.5 |
sum[0] = 0; |
417 |
jsr166 |
1.3 |
al.forEach(n -> sum[0] += n); |
418 |
|
|
check.sum(sum[0]);}}}, |
419 |
|
|
new Job("ArrayDeque.forEach") { |
420 |
|
|
public void work() throws Throwable { |
421 |
jsr166 |
1.5 |
int[] sum = new int[1]; |
422 |
jsr166 |
1.3 |
for (int i = 0; i < iterations; i++) { |
423 |
jsr166 |
1.5 |
sum[0] = 0; |
424 |
jsr166 |
1.3 |
ad.forEach(n -> sum[0] += n); |
425 |
|
|
check.sum(sum[0]);}}}, |
426 |
|
|
new Job("Vector.forEach") { |
427 |
|
|
public void work() throws Throwable { |
428 |
jsr166 |
1.5 |
int[] sum = new int[1]; |
429 |
jsr166 |
1.3 |
for (int i = 0; i < iterations; i++) { |
430 |
jsr166 |
1.5 |
sum[0] = 0; |
431 |
jsr166 |
1.3 |
v.forEach(n -> sum[0] += n); |
432 |
|
|
check.sum(sum[0]);}}}, |
433 |
|
|
new Job("ArrayList.iterator().forEachRemaining()") { |
434 |
|
|
public void work() throws Throwable { |
435 |
jsr166 |
1.5 |
int[] sum = new int[1]; |
436 |
jsr166 |
1.3 |
for (int i = 0; i < iterations; i++) { |
437 |
jsr166 |
1.5 |
sum[0] = 0; |
438 |
jsr166 |
1.3 |
al.iterator().forEachRemaining(n -> sum[0] += n); |
439 |
|
|
check.sum(sum[0]);}}}, |
440 |
|
|
new Job("ArrayDeque.descendingIterator().forEachRemaining()") { |
441 |
|
|
public void work() throws Throwable { |
442 |
jsr166 |
1.5 |
int[] sum = new int[1]; |
443 |
jsr166 |
1.3 |
for (int i = 0; i < iterations; i++) { |
444 |
jsr166 |
1.5 |
sum[0] = 0; |
445 |
jsr166 |
1.3 |
ad.descendingIterator().forEachRemaining(n -> sum[0] += n); |
446 |
|
|
check.sum(sum[0]);}}}, |
447 |
|
|
new Job("ArrayDeque.iterator().forEachRemaining()") { |
448 |
|
|
public void work() throws Throwable { |
449 |
jsr166 |
1.5 |
int[] sum = new int[1]; |
450 |
jsr166 |
1.3 |
for (int i = 0; i < iterations; i++) { |
451 |
jsr166 |
1.5 |
sum[0] = 0; |
452 |
jsr166 |
1.3 |
ad.iterator().forEachRemaining(n -> sum[0] += n); |
453 |
|
|
check.sum(sum[0]);}}}, |
454 |
|
|
new Job("Vector.iterator().forEachRemaining()") { |
455 |
|
|
public void work() throws Throwable { |
456 |
jsr166 |
1.5 |
int[] sum = new int[1]; |
457 |
jsr166 |
1.3 |
for (int i = 0; i < iterations; i++) { |
458 |
jsr166 |
1.5 |
sum[0] = 0; |
459 |
jsr166 |
1.3 |
v.iterator().forEachRemaining(n -> sum[0] += n); |
460 |
|
|
check.sum(sum[0]);}}}, |
461 |
|
|
new Job("ArrayList.spliterator().forEachRemaining()") { |
462 |
|
|
public void work() throws Throwable { |
463 |
jsr166 |
1.5 |
int[] sum = new int[1]; |
464 |
jsr166 |
1.3 |
for (int i = 0; i < iterations; i++) { |
465 |
jsr166 |
1.5 |
sum[0] = 0; |
466 |
jsr166 |
1.3 |
al.spliterator().forEachRemaining(n -> sum[0] += n); |
467 |
|
|
check.sum(sum[0]);}}}, |
468 |
|
|
new Job("ArrayDeque.spliterator().forEachRemaining()") { |
469 |
|
|
public void work() throws Throwable { |
470 |
jsr166 |
1.5 |
int[] sum = new int[1]; |
471 |
jsr166 |
1.3 |
for (int i = 0; i < iterations; i++) { |
472 |
jsr166 |
1.5 |
sum[0] = 0; |
473 |
jsr166 |
1.3 |
ad.spliterator().forEachRemaining(n -> sum[0] += n); |
474 |
|
|
check.sum(sum[0]);}}}, |
475 |
|
|
new Job("Vector.spliterator().forEachRemaining()") { |
476 |
|
|
public void work() throws Throwable { |
477 |
jsr166 |
1.5 |
int[] sum = new int[1]; |
478 |
jsr166 |
1.3 |
for (int i = 0; i < iterations; i++) { |
479 |
jsr166 |
1.5 |
sum[0] = 0; |
480 |
jsr166 |
1.3 |
v.spliterator().forEachRemaining(n -> sum[0] += n); |
481 |
|
|
check.sum(sum[0]);}}}, |
482 |
jsr166 |
1.8 |
new Job("ArrayList.spliterator().tryAdvance()") { |
483 |
|
|
public void work() throws Throwable { |
484 |
|
|
int[] sum = new int[1]; |
485 |
|
|
for (int i = 0; i < iterations; i++) { |
486 |
|
|
sum[0] = 0; |
487 |
|
|
Spliterator<Integer> spliterator = al.spliterator(); |
488 |
|
|
do {} while (spliterator.tryAdvance(n -> sum[0] += n)); |
489 |
|
|
check.sum(sum[0]);}}}, |
490 |
|
|
new Job("ArrayDeque.spliterator().tryAdvance()") { |
491 |
|
|
public void work() throws Throwable { |
492 |
|
|
int[] sum = new int[1]; |
493 |
|
|
for (int i = 0; i < iterations; i++) { |
494 |
|
|
sum[0] = 0; |
495 |
|
|
Spliterator<Integer> spliterator = ad.spliterator(); |
496 |
|
|
do {} while (spliterator.tryAdvance(n -> sum[0] += n)); |
497 |
|
|
check.sum(sum[0]);}}}, |
498 |
|
|
new Job("Vector.spliterator().tryAdvance()") { |
499 |
|
|
public void work() throws Throwable { |
500 |
|
|
int[] sum = new int[1]; |
501 |
|
|
for (int i = 0; i < iterations; i++) { |
502 |
|
|
sum[0] = 0; |
503 |
|
|
Spliterator<Integer> spliterator = v.spliterator(); |
504 |
|
|
do {} while (spliterator.tryAdvance(n -> sum[0] += n)); |
505 |
|
|
check.sum(sum[0]);}}}, |
506 |
jsr166 |
1.9 |
new Job("ArrayList.removeIf") { |
507 |
|
|
public void work() throws Throwable { |
508 |
|
|
int[] sum = new int[1]; |
509 |
|
|
for (int i = 0; i < iterations; i++) { |
510 |
|
|
sum[0] = 0; |
511 |
|
|
al.removeIf(n -> { sum[0] += n; return false; }); |
512 |
|
|
check.sum(sum[0]);}}}, |
513 |
|
|
new Job("ArrayDeque.removeIf") { |
514 |
|
|
public void work() throws Throwable { |
515 |
|
|
int[] sum = new int[1]; |
516 |
|
|
for (int i = 0; i < iterations; i++) { |
517 |
|
|
sum[0] = 0; |
518 |
|
|
ad.removeIf(n -> { sum[0] += n; return false; }); |
519 |
|
|
check.sum(sum[0]);}}}, |
520 |
|
|
new Job("Vector.removeIf") { |
521 |
|
|
public void work() throws Throwable { |
522 |
|
|
int[] sum = new int[1]; |
523 |
|
|
for (int i = 0; i < iterations; i++) { |
524 |
|
|
sum[0] = 0; |
525 |
|
|
v.removeIf(n -> { sum[0] += n; return false; }); |
526 |
|
|
check.sum(sum[0]);}}}, |
527 |
jsr166 |
1.12 |
new Job("ArrayList subList .removeIf") { |
528 |
|
|
public void work() throws Throwable { |
529 |
|
|
int[] sum = new int[1]; |
530 |
|
|
List<Integer> sl = asSubList(al); |
531 |
|
|
for (int i = 0; i < iterations; i++) { |
532 |
|
|
sum[0] = 0; |
533 |
|
|
sl.removeIf(n -> { sum[0] += n; return false; }); |
534 |
|
|
check.sum(sum[0]);}}}, |
535 |
jsr166 |
1.1 |
new Job("ArrayList subList get loop") { |
536 |
|
|
public void work() throws Throwable { |
537 |
|
|
List<Integer> sl = asSubList(al); |
538 |
|
|
for (int i = 0; i < iterations; i++) { |
539 |
|
|
int sum = 0; |
540 |
|
|
int size = sl.size(); |
541 |
|
|
for (int j = 0; j < size; ++j) |
542 |
|
|
sum += sl.get(j); |
543 |
|
|
check.sum(sum);}}}, |
544 |
|
|
new Job("ArrayList subList iterate for loop") { |
545 |
|
|
public void work() throws Throwable { |
546 |
|
|
for (int i = 0; i < iterations; i++) { |
547 |
|
|
int sum = 0; |
548 |
|
|
for (Integer n : asSubList(al)) |
549 |
|
|
sum += n; |
550 |
|
|
check.sum(sum);}}}, |
551 |
|
|
new Job("ArrayList subList subList subList iterate for loop") { |
552 |
|
|
public void work() throws Throwable { |
553 |
|
|
for (int i = 0; i < iterations; i++) { |
554 |
|
|
int sum = 0; |
555 |
|
|
for (Integer n : asSubList(asSubList(asSubList(al)))) |
556 |
|
|
sum += n; |
557 |
|
|
check.sum(sum);}}}, |
558 |
|
|
new Job("ArrayList backwards wrapper ListIterator for loop") { |
559 |
|
|
public void work() throws Throwable { |
560 |
|
|
for (int i = 0; i < iterations; i++) { |
561 |
|
|
int sum = 0; |
562 |
|
|
for (Integer n : backwards(al)) |
563 |
|
|
sum += n; |
564 |
|
|
check.sum(sum);}}}, |
565 |
|
|
new Job("ArrayList backwards wrapper subList ListIterator for loop") { |
566 |
|
|
public void work() throws Throwable { |
567 |
|
|
for (int i = 0; i < iterations; i++) { |
568 |
|
|
int sum = 0; |
569 |
|
|
for (Integer n : backwards(asSubList(al))) |
570 |
|
|
sum += n; |
571 |
|
|
check.sum(sum);}}}, |
572 |
|
|
// new Job("ArrayList iterate desugared") { |
573 |
|
|
// public void work() throws Throwable { |
574 |
|
|
// for (int i = 0; i < iterations; i++) { |
575 |
|
|
// int sum = 0; |
576 |
|
|
// for (Iterator<Integer> it = al.iterator(); it.hasNext();) |
577 |
|
|
// sum += it.next(); |
578 |
|
|
// check.sum(sum);}}}, |
579 |
|
|
new Job("Short ArrayList get loop") { |
580 |
|
|
public void work() throws Throwable { |
581 |
|
|
for (int i = 0; i < (iterations * size / shortSize); i++) { |
582 |
|
|
int sum = 0; |
583 |
|
|
int size = sal.size(); |
584 |
|
|
for (int j = 0; j < size; ++j) |
585 |
|
|
sum += sal.get(j); |
586 |
|
|
shortCheck.sum(sum);}}}, |
587 |
|
|
new Job("Short ArrayList iterate for loop") { |
588 |
|
|
public void work() throws Throwable { |
589 |
|
|
for (int i = 0; i < (iterations * size / shortSize); i++) { |
590 |
|
|
int sum = 0; |
591 |
|
|
for (Integer n : sal) |
592 |
|
|
sum += n; |
593 |
|
|
shortCheck.sum(sum);}}}, |
594 |
|
|
new Job("Short ArrayList sublist iterate for loop") { |
595 |
|
|
public void work() throws Throwable { |
596 |
|
|
for (int i = 0; i < (iterations * size / shortSize); i++) { |
597 |
|
|
int sum = 0; |
598 |
|
|
for (Integer n : asSubList(sal)) |
599 |
|
|
sum += n; |
600 |
|
|
shortCheck.sum(sum);}}}, |
601 |
|
|
new Job("Vector ArrayList alternating iteration") { |
602 |
|
|
public void work() throws Throwable { |
603 |
|
|
for (int i = 0; i < iterations; i++) { |
604 |
|
|
int sum = 0; |
605 |
|
|
Iterator<Integer> it1 = v.iterator(); |
606 |
|
|
Iterator<Integer> it2 = al.iterator(); |
607 |
|
|
while (it1.hasNext()) |
608 |
|
|
sum += it1.next() + it2.next(); |
609 |
|
|
check.sum(sum/2);}}}, |
610 |
|
|
new Job("Vector ArrayList alternating invokeVirtual iteration") { |
611 |
|
|
public void work() throws Throwable { |
612 |
|
|
for (int i = 0; i < iterations; i++) { |
613 |
|
|
int sum = 0; |
614 |
jsr166 |
1.16 |
List<Iterator<Integer>> its = new ArrayList<>(2); |
615 |
jsr166 |
1.1 |
its.add(v.iterator()); |
616 |
|
|
its.add(al.iterator()); |
617 |
|
|
for (int k = 0; its.get(k).hasNext(); k = (k == 0) ? 1 : 0) |
618 |
|
|
sum += its.get(k).next(); |
619 |
|
|
check.sum(sum/2);}}}, |
620 |
|
|
new Job("ConcurrentSkipListMap entrySet iterate") { |
621 |
|
|
public void work() throws Throwable { |
622 |
|
|
for (int i = 0; i < iterations; i++) { |
623 |
|
|
int sum = 0; |
624 |
|
|
for (Map.Entry<Integer,Integer> e : m.entrySet()) |
625 |
|
|
sum += e.getKey(); |
626 |
jsr166 |
1.14 |
deoptimize(sum);}}}, |
627 |
|
|
new Job("ArrayList.toArray()") { |
628 |
|
|
public void work() throws Throwable { |
629 |
|
|
int[] sum = new int[1]; |
630 |
|
|
for (int i = 0; i < iterations; i++) { |
631 |
|
|
sum[0] = 0; |
632 |
|
|
for (Object o : al.toArray()) |
633 |
|
|
sum[0] += (Integer) o; |
634 |
|
|
check.sum(sum[0]);}}}, |
635 |
|
|
new Job("ArrayList.toArray(a)") { |
636 |
|
|
public void work() throws Throwable { |
637 |
|
|
Integer[] a = new Integer[size]; |
638 |
|
|
int[] sum = new int[1]; |
639 |
|
|
for (int i = 0; i < iterations; i++) { |
640 |
|
|
sum[0] = 0; |
641 |
|
|
al.toArray(a); |
642 |
|
|
for (Object o : a) |
643 |
|
|
sum[0] += (Integer) o; |
644 |
|
|
check.sum(sum[0]);}}}, |
645 |
jsr166 |
1.18 |
new Job("ArrayList subList .toArray()") { |
646 |
|
|
public void work() throws Throwable { |
647 |
|
|
int[] sum = new int[1]; |
648 |
|
|
for (int i = 0; i < iterations; i++) { |
649 |
|
|
sum[0] = 0; |
650 |
|
|
for (Object o : asSubList(al).toArray()) |
651 |
|
|
sum[0] += (Integer) o; |
652 |
|
|
check.sum(sum[0]);}}}, |
653 |
|
|
new Job("ArrayList subList .toArray(a)") { |
654 |
|
|
public void work() throws Throwable { |
655 |
|
|
Integer[] a = new Integer[size]; |
656 |
|
|
int[] sum = new int[1]; |
657 |
|
|
for (int i = 0; i < iterations; i++) { |
658 |
|
|
sum[0] = 0; |
659 |
|
|
asSubList(al).toArray(a); |
660 |
|
|
for (Object o : a) |
661 |
|
|
sum[0] += (Integer) o; |
662 |
|
|
check.sum(sum[0]);}}}, |
663 |
jsr166 |
1.14 |
new Job("ArrayDeque.toArray()") { |
664 |
|
|
public void work() throws Throwable { |
665 |
|
|
int[] sum = new int[1]; |
666 |
|
|
for (int i = 0; i < iterations; i++) { |
667 |
|
|
sum[0] = 0; |
668 |
|
|
for (Object o : ad.toArray()) |
669 |
|
|
sum[0] += (Integer) o; |
670 |
|
|
check.sum(sum[0]);}}}, |
671 |
|
|
new Job("ArrayDeque.toArray(a)") { |
672 |
|
|
public void work() throws Throwable { |
673 |
|
|
Integer[] a = new Integer[size]; |
674 |
|
|
int[] sum = new int[1]; |
675 |
|
|
for (int i = 0; i < iterations; i++) { |
676 |
|
|
sum[0] = 0; |
677 |
|
|
ad.toArray(a); |
678 |
|
|
for (Object o : a) |
679 |
|
|
sum[0] += (Integer) o; |
680 |
|
|
check.sum(sum[0]);}}}, |
681 |
|
|
new Job("Vector.toArray()") { |
682 |
|
|
public void work() throws Throwable { |
683 |
|
|
int[] sum = new int[1]; |
684 |
|
|
for (int i = 0; i < iterations; i++) { |
685 |
|
|
sum[0] = 0; |
686 |
|
|
for (Object o : v.toArray()) |
687 |
|
|
sum[0] += (Integer) o; |
688 |
|
|
check.sum(sum[0]);}}}, |
689 |
|
|
new Job("Vector.toArray(a)") { |
690 |
|
|
public void work() throws Throwable { |
691 |
|
|
Integer[] a = new Integer[size]; |
692 |
|
|
int[] sum = new int[1]; |
693 |
|
|
for (int i = 0; i < iterations; i++) { |
694 |
|
|
sum[0] = 0; |
695 |
|
|
v.toArray(a); |
696 |
|
|
for (Object o : a) |
697 |
|
|
sum[0] += (Integer) o; |
698 |
|
|
check.sum(sum[0]);}}}, |
699 |
jsr166 |
1.1 |
}; |
700 |
|
|
|
701 |
|
|
time(filter(filter, jobs)); |
702 |
|
|
} |
703 |
|
|
} |