I just read about Branch-Prediction and wanted to try how this works with Java 8 Streams.
However the performance with Streams is always turning out to be wors
Long story short, Java programs can be accelerated by:
Yes!
for
cycle that is long enough for JIT to skip. Lambdas are typically small and can be compiled by JIT => there's possibility to gain performancefor
loop?Let's take a look at jdk/src/share/vm/runtime/globals.hpp
develop(intx, HugeMethodLimit, 8000,
"Don't compile methods larger than this if "
"+DontCompileHugeMethods")
If you have long enough cycle, it won't be compiled by JIT and will run slowly.
If you rewrite such a cycle to stream you'll probably use map
, filter
, flatMap
methods that split code to pieces and every piece can be small enough to fit under limit.
For sure, writing huge methods has other downsides apart from JIT compilation.
This scenario can be considered if, for example, you've got a lot of generated code.
Of course streams take advantage of branch prediction as every other code does. However branch prediction isn't the technology explicitly used to make streams faster AFAIK.
Never.
Premature optimization is the root of all evil ©Donald Knuth
Try to optimize algorithm instead. Streams are the interface for functional-like programming, not a tool to speedup loops.