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.PrimesFilter.t100;
  26 
  27 import org.openjdk.bench.java.util.stream.tasks.PrimesFilter.PrimesProblem;
  28 import org.openjdk.jmh.annotations.Benchmark;
  29 import org.openjdk.jmh.annotations.BenchmarkMode;
  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.State;
  34 
  35 import java.util.List;
  36 import java.util.concurrent.TimeUnit;
  37 import java.util.stream.Collectors;
  38 import java.util.stream.LongStream;
  39 
  40 /**
  41  * This benchmark evaluates find all prime numbers in a range.
  42  *
  43  * filter()...into() actions are benchmarked.
  44  */
  45 @BenchmarkMode(Mode.Throughput)
  46 @OutputTimeUnit(TimeUnit.SECONDS)
  47 @State(Scope.Benchmark)
  48 public class Lambda {
  49 
  50     private final long RANGE_START  = 1000_000_000_000_000L;
  51     private final long RANGE_END = RANGE_START + 100;
  52 
  53     @Benchmark
  54     public List<Long> bulk_seq_lambda() {
  55         return LongStream.range(RANGE_START, RANGE_END).boxed().filter(n -> PrimesProblem.isPrime(n)).collect(Collectors.<Long>toList());
  56     }
  57 
  58     @Benchmark
  59     public List<Long> bulk_seq_methodRef() {
  60         return LongStream.range(RANGE_START, RANGE_END).boxed().filter(PrimesProblem::isPrime).collect(Collectors.<Long>toList());
  61     }
  62 
  63     @Benchmark
  64     public List<Long> bulk_par_lambda() {
  65         return LongStream.range(RANGE_START, RANGE_END).parallel().boxed().filter(n -> PrimesProblem.isPrime(n)).collect(Collectors.<Long>toList());
  66     }
  67 
  68     @Benchmark
  69     public List<Long> bulk_par_methodRef() {
  70         return LongStream.range(RANGE_START, RANGE_END).parallel().boxed().filter(PrimesProblem::isPrime).collect(Collectors.<Long>toList());
  71     }
  72 
  73     @Benchmark
  74     public List<Long> bulk_parseq_lambda() {
  75         return LongStream.range(RANGE_START, RANGE_END).parallel().boxed().filter(n -> PrimesProblem.isPrime(n)).sequential().collect(Collectors.<Long>toList());
  76     }
  77 
  78     @Benchmark
  79     public List<Long> bulk_parseq_methodRef() {
  80         return LongStream.range(RANGE_START, RANGE_END).parallel().boxed().filter(PrimesProblem::isPrime).sequential().collect(Collectors.<Long>toList());
  81     }
  82 
  83 }