Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 742af25

Browse files
committed
Added measurements for Java 11 and added a (currently deactivated) further benchmark with streams
1 parent ba1a9fe commit 742af25

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

Readme.adoc

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ for (int i=0; i < names.length; i++) {
4242
Then, https://twitter.com/ReckordCarsten::[Carsten Reckord] https://twitter.com/ReckordCarsten/status/1231523609954455552:[suggested], that someone writes a microbenchmark to compare the three options.
4343
This is that microbenchmark suite.
4444

45+
[NOTE]
46+
====
47+
[#additonal-stream-solution]
48+
I decided to add one further alternative, which uses streams and may be more comparable to the other two solutions:
49+
50+
[source, java]
51+
----
52+
include::src/main/java/com/github/blalasaadri/MyBenchmark.java[tag=streams_in_one_go, indent=0]
53+
----
54+
The original stream solution looped through the names twice (once when mapping them and once when printing them).
55+
This solution only goes through them once (since `forEach` is the only https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps[terminal stream operation], whereas in the original solution there was also `collect`.)
56+
57+
This alternative is not contained in the original measurements below - in part because I only thought of this problem after I had already started measuring and in part because every new benchmark increases the runtime considerably.
58+
I will add the results for this new benchmark in separate gists.
59+
====
60+
4561
== Implementation
4662

4763
This microbenchmark suite uses https://openjdk.java.net/projects/code-tools/jmh/[JMH], a "`Java harness for building, running, and analysing nano/micro/milli/macro benchmarks written in Java and other languages targetting the JVM.`" footnote:[The article https://www.oracle.com/technical-resources/articles/java/architect-benchmarking.html:[Avoiding Benchmarking Pitfalls on the JVM] from July 2014 may be old, but it explains why using JMH helps avoid common mistakes when writing microbenchmarks.]
@@ -131,7 +147,12 @@ The benchmarks can then be run with:
131147
$ java -jar target/benchmarks.java
132148
----
133149

134-
WARNING: Running these benchmarks takes a long time. The total completion time for one call for me was about 50 minutes.
150+
[WARNING]
151+
====
152+
Running these benchmarks takes a long time.
153+
The total completion time for one call for me (with the original 3 benchmarks) was about 50 minutes.
154+
Having added a fourth benchmark (as described <<additonal-stream-solution, here>>) the time is even longer.
155+
====
135156

136157
== Measurements
137158

@@ -178,20 +199,31 @@ With the much longer array of names, the enhanced for loop clocked in at about 1
178199

179200
The complete output can be found in https://gist.github.com/blalasaadri/f9c6de09275438f4f3817e3e3a773d20:[this gist].
180201

181-
//== With Java 11
182-
//.Java version information for Java 11
183-
//[source, console]
184-
//----
185-
//$ java -version
186-
//openjdk version "11.0.6" 2020-01-14
187-
//OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu119.10.1)
188-
//OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu119.10.1, mixed mode, sharing)
189-
//----
190-
//
191-
//.Benchmark results
192-
//[source]
193-
//----
194-
//----
202+
== With Java 11
203+
.Java version information for Java 11
204+
[source, console]
205+
----
206+
$ java -version
207+
openjdk version "11.0.6" 2020-01-14
208+
OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu119.10.1)
209+
OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu119.10.1, mixed mode, sharing)
210+
----
211+
212+
.Benchmark results
213+
[source]
214+
----
215+
Benchmark (listVariant) Mode Cnt Score Error Units
216+
MyBenchmark.mapAndPrintWithStreams FIVE_NAMES thrpt 25 4296358,352 ± 545014,011 ops/s
217+
MyBenchmark.mapAndPrintWithStreams AUTO_GENERATED_NAMES thrpt 25 26803,661 ± 3479,115 ops/s
218+
MyBenchmark.printInEnhancedForLoop FIVE_NAMES thrpt 25 9348700,863 ± 1023949,109 ops/s
219+
MyBenchmark.printInEnhancedForLoop AUTO_GENERATED_NAMES thrpt 25 37878,684 ± 3026,570 ops/s
220+
MyBenchmark.printInOldSchoolForLoop FIVE_NAMES thrpt 25 9819339,718 ± 1230652,392 ops/s
221+
MyBenchmark.printInOldSchoolForLoop AUTO_GENERATED_NAMES thrpt 25 39678,611 ± 3593,970 ops/s
222+
----
223+
For the original, shorter list, the enhanced for loop had about 2.18 the throughput compared to the stream while the basic for loop had about 2.29 times the throughput.
224+
With the much longer array of names, the enhanced for loop clocked in at about 1.41 times the throughput of the stream solution basic for loop managed about 1.48 times the throughput.
225+
226+
The complete output can be found in https://gist.github.com/blalasaadri/d0b05f422cba32b71450c8f2b73a577c:[this gist].
195227

196228
//=== With Java 13
197229
//

src/main/java/com/github/blalasaadri/MyBenchmark.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@
3737
import java.io.StringWriter;
3838
import java.util.Arrays;
3939
import java.util.List;
40+
import java.util.concurrent.TimeUnit;
4041
import java.util.stream.Collectors;
4142
import java.util.stream.IntStream;
4243

4344
import static com.github.blalasaadri.MyBenchmark.ListVariant.FIVE_NAMES;
4445

4546
@State(Scope.Benchmark)
47+
@Measurement(
48+
iterations = 5,
49+
time = 10,
50+
timeUnit = TimeUnit.SECONDS
51+
)
4652
public class MyBenchmark {
4753

4854
@Param({"FIVE_NAMES", "AUTO_GENERATED_NAMES"})
@@ -83,6 +89,15 @@ public void mapAndPrintWithStreams(Blackhole blackhole) {
8389
// end::streams[]
8490
}
8591

92+
// @Benchmark
93+
public void mapAndPrintWithStreamsInOneGo(Blackhole blackhole) {
94+
// tag::streams_in_one_go[]
95+
IntStream.range(0, names.length)
96+
.mapToObj(index -> index + ": " + names[index])
97+
.forEach(blackhole::consume);
98+
// end::streams_in_one_go[]
99+
}
100+
86101
@Benchmark
87102
public void printInEnhancedForLoop(Blackhole blackhole) {
88103
// tag::enhanced_for[]

0 commit comments

Comments
 (0)