by Karunamon · 11y ago · 0 comments
Code is here: https://gist.github.com/Karunamon/abc6483ac1d08f6cc137.
The result was that streams were roughly 4x slower.
Still, I really like some of the new constructs that Java is getting - they make the language a bit more expressive, lack of which has always been my main gripe with the language.
When loops stop looking like loops, it makes it rather harder to find them.
Sure, code is more compact, but compactness is not an end in itself.
I also like how it removes boiler plate to handle different container types making it easier to switch to different ones.
The same thing is going to happen with Java 8 until the feature becomes less shiny.
That sounds like arrays benefited heavily from the easy branch prediction.
Of course since I've never used any of them, here's my terrible example with stream beating for loop.
https://gist.github.com/anonymous/2395fb0728e491bc54f5
Warmup Warming up done 9262288 # for loop 6156414 # stream Done
edit: tuning WARMUP_RUNS to something lower (like 500 vs 10k) and for loop wins consistently vs warmup runs at 10k.
If I put on -XX:+PrintCompilation I see some extra compilation output but I don't really understand the output. I assume some of this contributes but there's way more output then I expected tbh
4929 263 3 java.lang.invoke.LambdaForm$DMH/1581781576::invokeStatic_L_L (14 bytes) made not entrant
4929 339 4 java.util.function.Predicate::isEqual (20 bytes)edit: https://gist.github.com/anonymous/ed0d8f4a5c6553fe8435
Is there some way in this example it could not actually run the code here?
1) forEach driver method is receiving multiple types, it's not monomorphic 2) you may be hitting OSR compilations 3) for loop may hit range checks on each get() 4) for loop version warms the cache for the stream version and this benchmark is mem ref heavy
So, please try to use JMH to get more accurate picture. And, as mentioned, this isn't really testing for loop vs streams.
[1]: http://www.azulsystems.com/blog/cliff/2011-04-04-fixing-the-...
[2]: https://wiki.openjdk.java.net/display/HotSpot/MethodData
Looking forward to the day it will be in the reference JDK.
-XX:UnlockDiagnosticVMOptions -XX:+PrintInlining will tell you whether this inlined, and dumping the JIT asm can be done to see what was actually generated.
In any case, at least JITWatch, Solaris Studio and Intel Intel Amplifier do allow to look at the generated assembly.
Should have used JMH instead.