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.Setup; 32 import org.openjdk.jmh.annotations.State; 33 34 import java.util.Iterator; 35 import java.util.concurrent.TimeUnit; 36 import java.util.concurrent.atomic.LongAdder; 37 import java.util.function.LongConsumer; 38 import java.util.stream.LongStream; 39 40 /** 41 * Benchmark for forEach()/iterator()/into() operations; 42 * Testing which one is faster for semantically-equvalent operations. 43 */ 44 @BenchmarkMode(Mode.Throughput) 45 @OutputTimeUnit(TimeUnit.SECONDS) 46 @State(Scope.Thread) 47 public class Terminal { 48 49 /** 50 * Implementation notes: 51 * - parallel version requires thread-safe sink, we use the same for sequential version for better comparison 52 * - operations are explicit inner classes to untangle unwanted lambda effects 53 */ 54 55 @Param("100000") 56 private int size; 57 58 private LongAdder sink; 59 private LongConsumer block; 60 61 @Setup 62 public void setup() { 63 sink = new LongAdder(); 64 block = new LongConsumer() { 65 @Override 66 public void accept(long v) { 67 sink.add(v); 68 } 69 }; 70 } 71 72 @Benchmark 73 public long baseline_prim_acc() { 74 long s = 0; 75 for (long l = 0L; l < size; l++) { 76 s += l; 77 } 78 sink.add(s); 79 return sink.sum(); 80 } 81 82 @Benchmark 83 public long baseline_prim_sink() { 84 for (long l = 0L; l < size; l++) { 85 sink.add(l); 86 } 87 return sink.sum(); 88 } 89 90 @Benchmark 91 public long baseline_iterator_acc() { 92 long s = 0; 93 for (Iterator<Long> iterator = LongStream.range(0, size).boxed().iterator(); iterator.hasNext(); ) { 94 Long l = iterator.next(); 95 s += l; 96 } 97 sink.add(s); 98 return sink.sum(); 99 } 100 101 @Benchmark 102 public long baseline_iterator_sink() { 103 for (Iterator<Long> iterator = LongStream.range(0, size).boxed().iterator(); iterator.hasNext(); ) { 104 sink.add(iterator.next()); 105 } 106 return sink.sum(); 107 } 108 109 @Benchmark 110 public long seq_iterator() { 111 Iterator<Long> i = LongStream.range(0, size).boxed().iterator(); 112 while (i.hasNext()) { 113 sink.add(i.next()); 114 } 115 return sink.sum(); 116 } 117 118 @Benchmark 119 public long par_iterator() { 120 Iterator<Long> i = LongStream.range(0, size).parallel().boxed().iterator(); 121 while (i.hasNext()) { 122 sink.add(i.next()); 123 } 124 return sink.sum(); 125 } 126 127 @Benchmark 128 public long seq_forEach() { 129 LongStream.range(1, size).forEach(block); 130 return sink.sum(); 131 } 132 133 @Benchmark 134 public long par_forEach() { 135 LongStream.range(1, size).parallel().forEach(block); 136 return sink.sum(); 137 } 138 139 @Benchmark 140 public long seq_into() { 141 return LongStream.range(1, size) 142 .collect(LongAdder::new, LongAdder::add, (la1, la2) -> la1.add(la2.sum())).sum(); 143 } 144 145 @Benchmark 146 public long par_into() { 147 return LongStream.range(1, size).parallel() 148 .collect(LongAdder::new, LongAdder::add, (la1, la2) -> la1.add(la2.sum())).sum(); 149 } 150 151 }