Skip to content

Instantly share code, notes, and snippets.

@amaembo amaembo/Concat.java
Created Aug 19, 2016

Embed
What would you like to do?
Stream API benchmark
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
@BenchmarkMode(Mode.AverageTime)
@Fork(3)
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
public class Concat {
@Param({"10", "1000", "100000"})
private int n;
@Param({"true", "false"})
private boolean pollute;
private List<String> input;
@Setup
public void setup() {
input = Stream.generate(() -> new String("abcdef")).limit(n).collect(Collectors.toList());
if(pollute) {
for(int i=0; i<1000; i++) {
input.stream().map(s -> s+"1").collect(Collectors.toList());
input.stream().map(s -> s+"2").collect(Collectors.toList());
input.stream().map(s -> s+"3").collect(Collectors.toList());
}
}
}
@Benchmark
public List<String> plain() {
List<String> result = new ArrayList<>();
for(String s : input) result.add(s+"xyz");
return result;
}
@Benchmark
public List<String> stream() {
return input.stream().map(s -> s+"xyz").collect(Collectors.toList());
}
}
# JMH 1.13 (released 27 days ago)
# VM version: JDK 1.8.0_91, VM 25.91-b14
# VM invoker: C:\Program Files\Java\jre1.8.0_91\bin\java.exe
Benchmark (n) (pollute) Mode Cnt Score Error Units
Concat.stream 10 true avgt 15 0,335 ± 0,008 us/op
Concat.stream 10 false avgt 15 0,312 ± 0,006 us/op
Concat.stream 1000 true avgt 15 26,733 ± 0,740 us/op
Concat.stream 1000 false avgt 15 24,910 ± 0,477 us/op
Concat.stream 100000 true avgt 15 3127,121 ± 299,316 us/op
Concat.stream 100000 false avgt 15 2869,882 ± 86,572 us/op
Concat.plain 10 true avgt 15 0,254 ± 0,011 us/op
Concat.plain 10 false avgt 15 0,258 ± 0,013 us/op
Concat.plain 1000 true avgt 15 25,366 ± 0,438 us/op
Concat.plain 1000 false avgt 15 24,992 ± 0,705 us/op
Concat.plain 100000 true avgt 15 2829,553 ± 162,468 us/op
Concat.plain 100000 false avgt 15 2762,287 ± 122,223 us/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.