Skip to content

Instantly share code, notes, and snippets.

@mp911de
Last active February 11, 2022 20:04
Show Gist options
  • Save mp911de/7f203d787c6fdaf97dc91e6dc3aa73d3 to your computer and use it in GitHub Desktop.
Save mp911de/7f203d787c6fdaf97dc91e6dc3aa73d3 to your computer and use it in GitHub Desktop.
Benchmark using Optional/Streams vs. imperative variants of the same code
/*
* Copyright 2019 the original author or authors.
*
* Licensed 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.concurrent.TimeUnit;
import org.junit.platform.commons.annotation.Testable;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
/**
* @author Mark Paluch
*/
@Warmup(iterations = 3, time = 2)
@Measurement(iterations = 3, time = 2)
@Fork(value = 1)
@State(Scope.Thread)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@Testable
public class Benchmarks {
private final Source imperative = new ImperativeSource();
private final Source stream = new StreamSource();
@Benchmark
public Object getOptionalImperative() {
return imperative.getItem("thr").orElse("not found");
}
@Benchmark
public Object getOptionalStream() {
return stream.getItem("thr").orElse("not found");
}
@Benchmark
public Object getNullableImperative() {
String result = imperative.getNullableItem("thr");
return result == null ? "not found" : result;
}
@Benchmark
public Object getNullableStream() {
String result = stream.getNullableItem("thr");
return result == null ? "not found" : result;
}
}
/*
* Copyright 2019 the original author or authors.
*
* Licensed 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.Arrays;
import java.util.List;
import java.util.Optional;
/**
* @author Mark Paluch
*/
public class ImperativeSource implements Source {
private final List<MyItem> items = Arrays
.asList(new MyItem("one"), new MyItem("two"), new MyItem("three"));
@Override
public Optional<String> getItem(String filter) {
return Optional.ofNullable(getNullableItem(filter));
}
@Override
public String getNullableItem(String filter) {
for (MyItem item : items) {
if (item.getValue().startsWith(filter)) {
return item.getValue();
}
}
return null;
}
static class MyItem {
private final String value;
public MyItem(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
}
}
<?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>stream-optional-jmh</groupId>
<artifactId>stream-optional-jmh</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>com.github.mp911de.microbenchmark-runner</groupId>
<artifactId>microbenchmark-runner-junit5</artifactId>
<version>0.1.0.RELEASE</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
</project>
Java 8:
Benchmarks.getNullableImperative thrpt 3 100776671,872 ± 83086492,635 ops/s
Benchmarks.getNullableStream thrpt 3 15769775,360 ± 2679082,192 ops/s
Benchmarks.getOptionalImperative thrpt 3 103626247,773 ± 13883128,975 ops/s
Benchmarks.getOptionalStream thrpt 3 16060717,979 ± 4156670,804 ops/s
/*
* Copyright 2019 the original author or authors.
*
* Licensed 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.Arrays;
import java.util.List;
import java.util.Optional;
/**
* @author Mark Paluch
*/
public interface Source {
Optional<String> getItem(String filter);
String getNullableItem(String filter);
}
/*
* Copyright 2019 the original author or authors.
*
* Licensed 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.Arrays;
import java.util.List;
import java.util.Optional;
/**
* @author Mark Paluch
*/
public class StreamSource implements Source {
private final List<MyItem> items = Arrays
.asList(new MyItem("one"), new MyItem("two"), new MyItem("three"));
@Override
public Optional<String> getItem(String filter) {
return items.stream().map(MyItem::getValue).filter(it -> it.startsWith(filter))
.findFirst();
}
@Override
public String getNullableItem(String filter) {
return getItem(filter).orElse(null);
}
static class MyItem {
private final String value;
public MyItem(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
}
}
@mp911de
Copy link
Author

mp911de commented Jul 26, 2019

CPU Profile

CPU Profiling

Memory Profile

Memory Profiling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment