Created
November 28, 2024 13:45
-
-
Save amaembo/f1ca6fd922850eb56caab759d3d8ecc0 to your computer and use it in GitHub Desktop.
Stream test
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
Benchmark (size) Mode Cnt Score Error Units | |
StreamTest.wrapWithBrackets 10 avgt 30 151,229 � 15,301 ns/op | |
StreamTest.wrapWithBrackets 100 avgt 30 1041,827 � 29,505 ns/op | |
StreamTest.wrapWithBrackets 1000 avgt 30 11097,845 � 289,198 ns/op | |
StreamTest.wrapWithBracketsLoop 10 avgt 30 112,147 � 6,344 ns/op | |
StreamTest.wrapWithBracketsLoop 100 avgt 30 1329,546 � 55,875 ns/op | |
StreamTest.wrapWithBracketsLoop 1000 avgt 30 14293,504 � 2261,672 ns/op | |
Process finished with exit code 0 |
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 org.example; | |
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; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import java.util.stream.IntStream; | |
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) | |
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@BenchmarkMode(Mode.AverageTime) | |
@Fork(3) | |
@State(Scope.Benchmark) | |
public class StreamTest { | |
@Param({"10", "100", "1000"}) | |
private int size; | |
List<String> data; | |
@Setup | |
public void setup() { | |
data = IntStream.range(0, size).mapToObj(i -> "hello" + i).toList(); | |
} | |
@Benchmark | |
public List<String> wrapWithBrackets() { | |
return data.stream().map(x -> "[" + x + "]").toList(); | |
} | |
@Benchmark | |
public List<String> wrapWithBracketsLoop() { | |
List<String> list = new ArrayList<>(); | |
for (String x : data) { | |
list.add("[" + x + "]"); | |
} | |
return list; | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.include(StreamTest.class.getSimpleName()) | |
.build(); | |
new Runner(opt).run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment