|
1 /* |
|
2 * Copyright (c) 2014 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 package org.openjdk.bench.java.util.stream.pipeline; |
|
24 |
|
25 import org.openjdk.jmh.annotations.Benchmark; |
|
26 import org.openjdk.jmh.annotations.BenchmarkMode; |
|
27 import org.openjdk.jmh.annotations.Mode; |
|
28 import org.openjdk.jmh.annotations.OutputTimeUnit; |
|
29 import org.openjdk.jmh.annotations.Param; |
|
30 import org.openjdk.jmh.annotations.Scope; |
|
31 import org.openjdk.jmh.annotations.State; |
|
32 |
|
33 import java.util.concurrent.TimeUnit; |
|
34 import java.util.concurrent.atomic.LongAdder; |
|
35 import java.util.function.LongPredicate; |
|
36 import java.util.stream.LongStream; |
|
37 |
|
38 /** |
|
39 * Benchmark tests the pipeline fusion abilities. |
|
40 */ |
|
41 @BenchmarkMode(Mode.Throughput) |
|
42 @OutputTimeUnit(TimeUnit.SECONDS) |
|
43 @State(Scope.Thread) |
|
44 public class PipelineParMultiple { |
|
45 |
|
46 @Param("100000") |
|
47 private int size; |
|
48 |
|
49 @Benchmark |
|
50 public long bulk_into_anon() { |
|
51 return LongStream.range(0, size).parallel() |
|
52 .filter((x) -> true) |
|
53 .filter((x) -> true) |
|
54 .filter((x) -> true) |
|
55 .filter((x) -> true) |
|
56 .filter((x) -> true) |
|
57 .filter((x) -> true) |
|
58 .filter((x) -> true) |
|
59 .filter((x) -> false) |
|
60 .collect(LongAdder::new, LongAdder::add, (la1, la2) -> la1.add(la2.sum())).sum(); |
|
61 } |
|
62 |
|
63 @Benchmark |
|
64 public long bulk_into_named() { |
|
65 LongPredicate t = (x) -> true; |
|
66 LongPredicate f = (x) -> false; |
|
67 return LongStream.range(0, size).parallel() |
|
68 .filter(t) |
|
69 .filter(t) |
|
70 .filter(t) |
|
71 .filter(t) |
|
72 .filter(t) |
|
73 .filter(t) |
|
74 .filter(t) |
|
75 .filter(f) |
|
76 .collect(LongAdder::new, LongAdder::add, (la1, la2) -> la1.add(la2.sum())).sum(); |
|
77 } |
|
78 |
|
79 |
|
80 @Benchmark |
|
81 public long bulk_foreach_anon() { |
|
82 LongAdder adder = new LongAdder(); |
|
83 LongStream.range(0, size).parallel().forEach((l) -> { |
|
84 if (((LongPredicate) (x) -> true).test(l)) |
|
85 if (((LongPredicate) (x) -> true).test(l)) |
|
86 if (((LongPredicate) (x) -> true).test(l)) |
|
87 if (((LongPredicate) (x) -> true).test(l)) |
|
88 if (((LongPredicate) (x) -> true).test(l)) |
|
89 if (((LongPredicate) (x) -> true).test(l)) |
|
90 if (((LongPredicate) (x) -> true).test(l)) |
|
91 if (((LongPredicate) (x) -> false).test(l)) |
|
92 adder.add(l); |
|
93 }); |
|
94 return adder.sum(); |
|
95 } |
|
96 |
|
97 |
|
98 @Benchmark |
|
99 public long bulk_foreach_named() { |
|
100 LongAdder adder = new LongAdder(); |
|
101 LongPredicate t = (x) -> true; |
|
102 LongPredicate f = (x) -> false; |
|
103 LongStream.range(0, size).parallel().forEach((l) -> { |
|
104 if (t.test(l)) |
|
105 if (t.test(l)) |
|
106 if (t.test(l)) |
|
107 if (t.test(l)) |
|
108 if (t.test(l)) |
|
109 if (t.test(l)) |
|
110 if (t.test(l)) |
|
111 if (f.test(l)) |
|
112 adder.add(l); |
|
113 }); |
|
114 return adder.sum(); |
|
115 } |
|
116 |
|
117 @Benchmark |
|
118 public long bulk_ifs() { |
|
119 LongAdder adder = new LongAdder(); |
|
120 LongStream.range(0, size).parallel().forEach((l) -> { |
|
121 if (true) |
|
122 if (true) |
|
123 if (true) |
|
124 if (true) |
|
125 if (true) |
|
126 if (true) |
|
127 if (true) |
|
128 if (false) |
|
129 adder.add(l); |
|
130 }); |
|
131 return adder.sum(); |
|
132 } |
|
133 |
|
134 } |