Last active
September 9, 2021 08:18
-
-
Save ptupitsyn/5934bbbf8f92ac4937e534af9386da97 to your computer and use it in GitHub Desktop.
Java streams vs loops benchmark
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.example</groupId> | |
<artifactId>JavaStreamsBenchmark</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<properties> | |
<maven.compiler.source>11</maven.compiler.source> | |
<maven.compiler.target>11</maven.compiler.target> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.openjdk.jmh</groupId> | |
<artifactId>jmh-core</artifactId> | |
<version>1.32</version> | |
</dependency> | |
<dependency> | |
<groupId>org.openjdk.jmh</groupId> | |
<artifactId>jmh-generator-annprocess</artifactId> | |
<version>1.32</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains 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
/* | |
* Licensed to the Apache Software Foundation (ASF) under one or more | |
* contributor license agreements. See the NOTICE file distributed with | |
* this work for additional information regarding copyright ownership. | |
* The ASF licenses this file to You under the Apache License, Version 2.0 | |
* (the "License"); you may not use this file except in compliance with | |
* the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package benchmarks; | |
import java.util.ArrayList; | |
import java.util.UUID; | |
import java.util.concurrent.TimeUnit; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.infra.Blackhole; | |
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; | |
import org.openjdk.jmh.runner.options.TimeValue; | |
/** | |
* Benchmark Mode Cnt Score Error Units | |
* StreamVsLoopBenchmark.loopSum thrpt 3 6412.790 ± 165.033 ops/ms | |
* StreamVsLoopBenchmark.loopSum:·gc.alloc.rate thrpt 3 ≈ 10⁻⁴ MB/sec | |
* StreamVsLoopBenchmark.loopSum:·gc.alloc.rate.norm thrpt 3 ≈ 10⁻⁵ B/op | |
* StreamVsLoopBenchmark.loopSum:·gc.count thrpt 3 ≈ 0 counts | |
* StreamVsLoopBenchmark.streamSum thrpt 3 1049.772 ± 9.228 ops/ms | |
* StreamVsLoopBenchmark.streamSum:·gc.alloc.rate thrpt 3 312.699 ± 2.780 MB/sec | |
* StreamVsLoopBenchmark.streamSum:·gc.alloc.rate.norm thrpt 3 328.000 ± 0.001 B/op | |
* StreamVsLoopBenchmark.streamSum:·gc.churn.G1_Eden_Space thrpt 3 312.496 ± 179.564 MB/sec | |
* StreamVsLoopBenchmark.streamSum:·gc.churn.G1_Eden_Space.norm thrpt 3 327.784 ± 185.407 B/op | |
* StreamVsLoopBenchmark.streamSum:·gc.churn.G1_Old_Gen thrpt 3 0.001 ± 0.003 MB/sec | |
* StreamVsLoopBenchmark.streamSum:·gc.churn.G1_Old_Gen.norm thrpt 3 0.001 ± 0.003 B/op | |
* StreamVsLoopBenchmark.streamSum:·gc.count thrpt 3 55.000 counts | |
* StreamVsLoopBenchmark.streamSum:·gc.time thrpt 3 28.000 ms | |
* | |
* (JDK 11.0.11, OpenJDK 64-Bit Server VM, 11.0.11+9-Ubuntu-0ubuntu2.20.04, i7-7700HQ) | |
*/ | |
@State(Scope.Benchmark) | |
public class StreamVsLoopBenchmark { | |
private ArrayList<String> data; | |
@Setup | |
public void setup() { | |
data = new ArrayList<>(); | |
for (int i = 0; i < 100; i++) { | |
data.add(UUID.randomUUID().toString()); | |
} | |
} | |
@SuppressWarnings("all") | |
@Benchmark | |
@BenchmarkMode(Mode.Throughput) | |
@OutputTimeUnit(TimeUnit.MILLISECONDS) | |
public void streamSum(Blackhole blackhole) { | |
var sum = data.stream().filter(x -> x.charAt(0) != 'x').mapToInt(x -> x.length()).sum(); | |
blackhole.consume(sum); | |
} | |
@SuppressWarnings("all") | |
@Benchmark | |
@BenchmarkMode(Mode.Throughput) | |
@OutputTimeUnit(TimeUnit.MILLISECONDS) | |
public void loopSum(Blackhole blackhole) { | |
int sum = 0; | |
for (String s : data) { | |
if (s.charAt(0) != 'x') { | |
sum += s.length(); | |
} | |
} | |
blackhole.consume(sum); | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.include(StreamVsLoopBenchmark.class.getSimpleName()) | |
.warmupIterations(2) | |
.warmupTime(TimeValue.seconds(10)) | |
.measurementIterations(3) | |
.measurementTime(TimeValue.seconds(10)) | |
.forks(1) | |
.addProfiler("gc") | |
.build(); | |
new Runner(opt).run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment