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.tasks.PrimesFilter.t100; 24 25 import org.openjdk.bench.java.util.stream.tasks.PrimesFilter.PrimesProblem; 26 import org.openjdk.jmh.annotations.Benchmark; 27 import org.openjdk.jmh.annotations.BenchmarkMode; 28 import org.openjdk.jmh.annotations.Mode; 29 import org.openjdk.jmh.annotations.OutputTimeUnit; 30 import org.openjdk.jmh.annotations.Scope; 31 import org.openjdk.jmh.annotations.State; 32 33 import java.util.ArrayList; 34 import java.util.Collections; 35 import java.util.List; 36 import java.util.concurrent.RecursiveTask; 37 import java.util.concurrent.TimeUnit; 38 import java.util.function.Predicate; 39 import java.util.stream.Collectors; 40 import java.util.stream.LongStream; 41 42 /** 43 * This benchmark evaluates find all prime numbers in a range. 44 * 45 * filter()...into() actions are benchmarked. 46 */ 47 @BenchmarkMode(Mode.Throughput) 48 @OutputTimeUnit(TimeUnit.SECONDS) 49 @State(Scope.Benchmark) 50 public class Bulk { 51 52 private final long RANGE_START = 1000_000_000_000_000L; 53 private final long RANGE_END = RANGE_START + 100; 54 55 @Benchmark 56 public List<Long> hm_seq() { 57 List<Long> results = new ArrayList<>(); 58 for (long i = RANGE_START; i < RANGE_END; i++) { 59 if (PrimesProblem.isPrime(i)) { 60 results.add(i); 61 } 62 } 63 return results; 64 } 65 66 @Benchmark 67 public List<Long> hm_par() { 68 return new FactoringTask(RANGE_START, RANGE_END).invoke(); 69 } 70 71 @Benchmark 72 public List<Long> bulk_seq_inner() { 73 return LongStream.range(RANGE_START, RANGE_END) 74 .boxed() 75 .filter(new Predicate<Long>() { 76 @Override 77 public boolean test(Long o) { 78 return PrimesProblem.isPrime(o); 79 } 80 } 81 ).collect(Collectors.<Long>toList()); 82 } 83 84 @Benchmark 85 public List<Long> bulk_par_inner() { 86 return LongStream.range(RANGE_START, RANGE_END).parallel() 87 .boxed() 88 .filter(new Predicate<Long>() { 89 @Override 90 public boolean test(Long o) { 91 return PrimesProblem.isPrime(o); 92 } 93 } 94 ).collect(Collectors.<Long>toList()); 95 } 96 97 @Benchmark 98 public List<Long> bulk_parseq_inner() { 99 return LongStream.range(RANGE_START, RANGE_END).parallel() 100 .boxed() 101 .filter(new Predicate<Long>() { 102 @Override 103 public boolean test(Long o) { 104 return PrimesProblem.isPrime(o); 105 } 106 } 107 ).sequential().collect(Collectors.<Long>toList()); 108 } 109 110 public static class FactoringTask extends RecursiveTask<List<Long>> { 111 final long low; 112 final long high; 113 114 @Override 115 protected List<Long> compute() { 116 if (high - low == 1L) { 117 if (PrimesProblem.isPrime(low)) 118 return Collections.singletonList(low); 119 else 120 return Collections.emptyList(); 121 } 122 123 long mid = (low + high) / 2L; 124 FactoringTask t1 = new FactoringTask(low, mid); 125 FactoringTask t2 = new FactoringTask(mid, high); 126 127 List<Long> results; 128 129 // The right way to do it. Forks off one task and 130 // continues the other task in this thread. I've 131 // seen up to 8x speed up on 16-way Intel and 32-way 132 // SPARC boxes (which probably matches the actual number 133 // of cores they have, as opposed to the number of threads) 134 t2.fork(); 135 results = new ArrayList<>(t1.compute()); 136 results.addAll(t2.join()); 137 138 return results; 139 } 140 141 FactoringTask(long low, long high) { 142 this.low = low; 143 this.high = high; 144 } 145 } 146 147 }