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.DictionaryWordValue; 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 import java.util.function.Function; 41 42 43 /** 44 * Bulk scenario: searching max "wordValue" through the dictionary (array of strings). 45 */ 46 @BenchmarkMode(Mode.Throughput) 47 @OutputTimeUnit(TimeUnit.SECONDS) 48 @State(Scope.Benchmark) 49 public class Bulk { 50 51 @Setup(Level.Trial) 52 public void loadData() { 53 // cause classload and problem initialization 54 DictionaryProblem.class.getName(); 55 } 56 57 @Benchmark 58 public int hm_seq() { 59 int max = 0; 60 for (String s : DictionaryProblem.get()) { 61 int v = DictionaryProblem.wordValue(s); 62 if (v > max) { 63 max = v; 64 } 65 } 66 return max; 67 } 68 69 @Benchmark 70 public int hm_par() { 71 return new Task(DictionaryProblem.get(), 0, DictionaryProblem.get().length).invoke(); 72 } 73 74 @Benchmark 75 public int bulk_seq_inner() { 76 return Arrays.stream(DictionaryProblem.get()) 77 .map(new Function<String, Integer>() { 78 @Override 79 public Integer apply(String s) { 80 return DictionaryProblem.wordValue(s); 81 } 82 }) 83 .reduce(0, new BinaryOperator<Integer>() { 84 @Override 85 public Integer apply(Integer l, Integer r) { 86 return l > r ? l : r; 87 } 88 }); 89 } 90 91 @Benchmark 92 public int bulk_par_inner() { 93 return Arrays.stream(DictionaryProblem.get()).parallel() 94 .map(new Function<String, Integer>() { 95 @Override 96 public Integer apply(String s) { 97 return DictionaryProblem.wordValue(s); 98 } 99 }) 100 .reduce(0, new BinaryOperator<Integer>() { 101 @Override 102 public Integer apply(Integer l, Integer r) { 103 return l > r ? l : r; 104 } 105 }); 106 } 107 108 109 public static class Task extends RecursiveTask<Integer> { 110 private static final int FORK_LIMIT = 500; 111 112 private final String[] words; 113 private final int start, end; 114 115 Task(String[] w, int start, int end) { 116 this.words = w; 117 this.start = start; 118 this.end = end; 119 } 120 121 @Override 122 protected Integer compute() { 123 int size = end - start; 124 if (size > FORK_LIMIT) { 125 int mid = start + size / 2; 126 Task t1 = new Task(words, start, mid); 127 Task t2 = new Task(words, mid, end); 128 t1.fork(); 129 Integer v2 = t2.invoke(); 130 Integer v1 = t1.join(); 131 return Math.max(v1, v2); 132 } else { 133 int max = 0; 134 for (int i = start; i < end; i++) { 135 int v = DictionaryProblem.wordValue(words[i]); 136 if (v > max) { 137 max = v; 138 } 139 } 140 return max; 141 } 142 } 143 } 144 145 146 }