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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.openjdk.bench.java.util.stream.tasks.IntegerSum; 26 27 import org.openjdk.jmh.annotations.Benchmark; 28 import org.openjdk.jmh.annotations.BenchmarkMode; 29 import org.openjdk.jmh.annotations.Level; 30 import org.openjdk.jmh.annotations.Mode; 31 import org.openjdk.jmh.annotations.OutputTimeUnit; 32 import org.openjdk.jmh.annotations.Scope; 33 import org.openjdk.jmh.annotations.Setup; 34 import org.openjdk.jmh.annotations.State; 35 36 import java.util.Arrays; 37 import java.util.concurrent.RecursiveTask; 38 import java.util.concurrent.TimeUnit; 39 import java.util.function.BinaryOperator; 40 41 /** 42 * Bulk scenario: compute the sum of Integer array. 43 * 44 * This test covers bulk infrastructure only. Refer to other tests for lambda-specific cases. 45 */ 46 @BenchmarkMode(Mode.Throughput) 47 @OutputTimeUnit(TimeUnit.SECONDS) 48 @State(Scope.Benchmark) 49 public class Bulk { 50 51 private IntegerSumProblem problem; 52 53 @Setup(Level.Trial) 54 public void populateData(){ 55 problem = new IntegerSumProblem(); 56 } 57 58 @Benchmark 59 public int hm_seq() { 60 int sum = 0; 61 for (int v : problem.get()) { 62 sum += v; 63 } 64 return sum; 65 } 66 67 @Benchmark 68 public int hm_par() { 69 return new SumIntTask(problem.get()).invoke(); 70 } 71 72 @Benchmark 73 public int bulk_seq_inner() { 74 return Arrays.stream(problem.get()).reduce(0, new BinaryOperator<Integer>() { 75 @Override 76 public Integer apply(Integer left, Integer right) { 77 return left + right; 78 } 79 }); 80 } 81 82 @Benchmark 83 public int bulk_par_inner() { 84 return Arrays.stream(problem.get()).parallel().reduce(0, new BinaryOperator<Integer>() { 85 @Override 86 public Integer apply(Integer left, Integer right) { 87 return left + right; 88 } 89 }); 90 } 91 92 static class SumIntTask extends RecursiveTask<Integer> { 93 private static final int FORK_LIMIT = 1000; 94 final Integer[] data; 95 final int start, end; 96 97 SumIntTask(Integer[] data) { 98 this(data, 0, data.length); 99 } 100 101 SumIntTask(Integer[] data, int start, int end) { 102 this.data = data; 103 this.start = start; 104 this.end = end; 105 } 106 107 @Override 108 protected Integer compute() { 109 int size = end - start; 110 if (size > FORK_LIMIT) { 111 int mid = start + size / 2; 112 SumIntTask t1 = new SumIntTask(data, start, mid); 113 SumIntTask t2 = new SumIntTask(data, mid, end); 114 t1.fork(); 115 return t2.invoke() + t1.join(); 116 } else { 117 int sum = 0; 118 for (int i = start; i < end; i++) { 119 sum += data[i]; 120 } 121 return sum; 122 } 123 } 124 } 125 126 }