Created
October 29, 2025 12:06
-
-
Save Ladicek/3afff8bd4b139f168593c7f9bdd41f62 to your computer and use it in GitHub Desktop.
StreamIsEmptyBenchmark.java
This file contains hidden or 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
| package io.quarkus.arc.benchmarks; | |
| 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.Setup; | |
| import org.openjdk.jmh.annotations.State; | |
| import org.openjdk.jmh.annotations.Warmup; | |
| import java.util.concurrent.TimeUnit; | |
| import java.util.stream.Stream; | |
| @BenchmarkMode(Mode.Throughput) | |
| @Fork(5) | |
| @Warmup(iterations = 5, time = 1, batchSize = 8192) | |
| @Measurement(iterations = 5, time = 1, batchSize = 8192) | |
| @OutputTimeUnit(TimeUnit.SECONDS) | |
| @State(Scope.Benchmark) | |
| public class StreamIsEmptyBenchmark { | |
| private Stream<String> emptyStream() { | |
| return Stream.empty(); | |
| } | |
| private Stream<String> smallStream() { | |
| return Stream.of("foo", "bar", "baz"); | |
| } | |
| private Stream<String> mediumStream() { | |
| String[] mediumArray = new String[30]; | |
| for (int i = 0; i < mediumArray.length; i++) { | |
| mediumArray[i] = "str" + i; | |
| } | |
| return Stream.of(mediumArray); | |
| } | |
| private Stream<String> bigStream() { | |
| String[] bigArray = new String[300]; | |
| for (int i = 0; i < bigArray.length; i++) { | |
| bigArray[i] = "str" + i; | |
| } | |
| return Stream.of(bigArray); | |
| } | |
| @Benchmark | |
| public Stream<String> emptyStream_construct() { | |
| return emptyStream(); | |
| } | |
| @Benchmark | |
| public boolean emptyStream_findAny() { | |
| return emptyStream().findAny().isEmpty(); | |
| } | |
| @Benchmark | |
| public boolean emptyStream_count() { | |
| return emptyStream().count() == 0; | |
| } | |
| @Benchmark | |
| public Stream<String> smallStream_construct() { | |
| return smallStream(); | |
| } | |
| @Benchmark | |
| public boolean smallStream_findAny() { | |
| return smallStream().findAny().isEmpty(); | |
| } | |
| @Benchmark | |
| public boolean smallStream_count() { | |
| return smallStream().count() == 0; | |
| } | |
| @Benchmark | |
| public Stream<String> mediumStream_construct() { | |
| return mediumStream(); | |
| } | |
| @Benchmark | |
| public boolean mediumStream_findAny() { | |
| return mediumStream().findAny().isEmpty(); | |
| } | |
| @Benchmark | |
| public boolean mediumStream_count() { | |
| return mediumStream().count() == 0; | |
| } | |
| @Benchmark | |
| public Stream<String> bigStream_construct() { | |
| return bigStream(); | |
| } | |
| @Benchmark | |
| public boolean bigStream_findAny() { | |
| return bigStream().findAny().isEmpty(); | |
| } | |
| @Benchmark | |
| public boolean bigStream_count() { | |
| return bigStream().count() == 0; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment