You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 17, 2021. It is now read-only.
Copy file name to clipboardExpand all lines: Readme.adoc
+47-15Lines changed: 47 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,6 +42,22 @@ for (int i=0; i < names.length; i++) {
42
42
Then, https://twitter.com/ReckordCarsten::[Carsten Reckord] https://twitter.com/ReckordCarsten/status/1231523609954455552:[suggested], that someone writes a microbenchmark to compare the three options.
43
43
This is that microbenchmark suite.
44
44
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:
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
+
45
61
== Implementation
46
62
47
63
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:
131
147
$ java -jar target/benchmarks.java
132
148
----
133
149
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
+
====
135
156
136
157
== Measurements
137
158
@@ -178,20 +199,31 @@ With the much longer array of names, the enhanced for loop clocked in at about 1
178
199
179
200
The complete output can be found in https://gist.github.com/blalasaadri/f9c6de09275438f4f3817e3e3a773d20:[this gist].
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].
0 commit comments