As part of my investigation on the difference between using a complex filter or multiple filters in streams, I notice that performance on Java 12 is way slower than on Java 8.
Is any explanation for those weird results? Did I miss something here?
Configuration:
java 8
- OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-2~deb9u1-b13)
- OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
java 12
- OpenJDK Runtime Environment (build 12+33)
- OpenJDK 64-Bit Server VM (build 12+33, mixed mode, sharing)
VM options:
-XX:+UseG1GC
-server
-Xmx1024m
-Xms1024m
- CPU: 8 cores
JMH Throughput Results:
- Warmup: 10 iterations, 1 s each
- Measurement: 10 iterations, 1 s each
- Threads: 1 thread, will synchronize iterations
- Units: ops/s
Code
Stream + complex filter
public void complexFilter(ExecutionPlan plan, Blackhole blackhole) {
long count = plan.getDoubles()
.stream()
.filter(d -> d < Math.PI
&& d > Math.E
&& d != 3
&& d != 2)
.count();
blackhole.consume(count);
}
Stream + multiple filters
public void multipleFilters(ExecutionPlan plan, Blackhole blackhole) {
long count = plan.getDoubles()
.stream()
.filter(d -> d > Math.PI)
.filter(d -> d < Math.E)
.filter(d -> d != 3)
.filter(d -> d != 2)
.count();
blackhole.consume(count);
}
Parallel stream + complex filter
public void complexFilterParallel(ExecutionPlan plan, Blackhole blackhole) {
long count = plan.getDoubles()
.stream()
.parallel()
.filter(d -> d < Math.PI
&& d > Math.E
&& d != 3
&& d != 2)
.count();
blackhole.consume(count);
}
Parallel stream + multiple filters
public void multipleFiltersParallel(ExecutionPlan plan, Blackhole blackhole) {
long count = plan.getDoubles()
.stream()
.parallel()
.filter(d -> d > Math.PI)
.filter(d -> d < Math.E)
.filter(d -> d != 3)
.filter(d -> d != 2)
.count();
blackhole.consume(count);
}
Old fashion java iteration
public void oldFashionFilters(ExecutionPlan plan, Blackhole blackhole) {
long count = 0;
for (int i = 0; i < plan.getDoubles().size(); i++) {
if (plan.getDoubles().get(i) > Math.PI
&& plan.getDoubles().get(i) > Math.E
&& plan.getDoubles().get(i) != 3
&& plan.getDoubles().get(i) != 2) {
count = count + 1;
}
}
blackhole.consume(count);
}
You can try by yourself by running docker command:
For Java 8:
docker run -it volkodav/java-filter-benchmark:java8
For Java 12:
docker run -it volkodav/java-filter-benchmark:java12
Source code: