46 |
|
import java.util.concurrent.ArrayBlockingQueue; |
47 |
|
import java.util.concurrent.ConcurrentLinkedDeque; |
48 |
|
import java.util.concurrent.ConcurrentLinkedQueue; |
49 |
+ |
import java.util.concurrent.CopyOnWriteArrayList; |
50 |
|
import java.util.concurrent.LinkedBlockingDeque; |
51 |
|
import java.util.concurrent.LinkedBlockingQueue; |
52 |
|
import java.util.concurrent.LinkedTransferQueue; |
56 |
|
import java.util.concurrent.TimeUnit; |
57 |
|
import java.util.concurrent.atomic.LongAdder; |
58 |
|
import java.util.regex.Pattern; |
59 |
+ |
import java.util.stream.Stream; |
60 |
|
|
61 |
|
/** |
62 |
|
* Usage: [iterations=N] [size=N] [filter=REGEXP] [warmup=SECONDS] |
272 |
|
|
273 |
|
ArrayList<Job> jobs = new ArrayList<>(); |
274 |
|
|
275 |
< |
List.<Collection<Integer>>of( |
275 |
> |
Stream.<Collection<Integer>>of( |
276 |
|
al, ad, abq, |
277 |
|
new LinkedList<>(al), |
278 |
|
new PriorityQueue<>(al), |
279 |
|
new Vector<>(al), |
280 |
+ |
new CopyOnWriteArrayList<>(al), |
281 |
|
new ConcurrentLinkedQueue<>(al), |
282 |
|
new ConcurrentLinkedDeque<>(al), |
283 |
|
new LinkedBlockingQueue<>(al), |
284 |
|
new LinkedBlockingDeque<>(al), |
285 |
|
new LinkedTransferQueue<>(al), |
286 |
< |
new PriorityBlockingQueue<>(al)).forEach( |
287 |
< |
x -> { |
288 |
< |
jobs.addAll(collectionJobs(x)); |
289 |
< |
if (x instanceof Deque) |
290 |
< |
jobs.addAll(dequeJobs((Deque<Integer>)x)); |
291 |
< |
}); |
286 |
> |
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 |
|
|
295 |
|
if (reverse) Collections.reverse(jobs); |
296 |
|
if (shuffle) Collections.shuffle(jobs); |
460 |
|
x.descendingIterator().forEachRemaining(n -> sum[0] += n); |
461 |
|
check.sum(sum[0]);}}}); |
462 |
|
} |
463 |
+ |
|
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 |
|
} |