BAEL-4876: Add benchmark
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class DifferentSourceSplitting {
|
||||
|
||||
private static final List<Integer> arrayListOfNumbers = new ArrayList<>();
|
||||
private static final List<Integer> linkedListOfNumbers = new LinkedList<>();
|
||||
|
||||
static {
|
||||
IntStream.rangeClosed(1, 100_000_000).forEach(i -> {
|
||||
arrayListOfNumbers.add(i);
|
||||
linkedListOfNumbers.add(i);
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void arrayListSequential() {
|
||||
arrayListOfNumbers.stream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void arrayListParallel() {
|
||||
arrayListOfNumbers.parallelStream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void linkedListSequential() {
|
||||
linkedListOfNumbers.stream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void linkedListParallel() {
|
||||
linkedListOfNumbers.parallelStream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class SplittingCosts {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void arrayListSequential() {
|
||||
IntStream.rangeClosed(1, 1_000).reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void arrayListParallel() {
|
||||
IntStream.rangeClosed(1, 1_000).parallel().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user