Created
August 5, 2017 07:41
-
-
Save mmimica/f7f7c8033820dd95dd449cf58e0ad58b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mmimica; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import java.util.stream.Stream; | |
import org.openjdk.jmh.annotations.*; | |
@State(Scope.Benchmark) | |
@BenchmarkMode(Mode.Throughput) | |
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) | |
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | |
@Fork(1) | |
public class BenchmarkStreams { | |
List<Integer> list = new ArrayList<>(); | |
@Param({ "1", "1000", "10000", "100000" }) | |
int size; | |
@Setup | |
public void setup() { | |
for (int i = 0; i < size; i++) | |
list.add(i); | |
} | |
private int find(Stream<Integer> stream) { | |
return stream | |
.filter(i -> i == -1) | |
.findAny() | |
.orElse(0); | |
} | |
@Benchmark | |
public int parallel_stream() { | |
return find(list.parallelStream()); | |
} | |
@Benchmark | |
public int stream() { | |
return find(list.stream()); | |
} | |
@Benchmark | |
public int loop() { | |
for (Integer i : list) { | |
if (i == -1) | |
return i; | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment