When should streams be preferred over traditional loops for best performance? Do streams take advantage of branch-prediction?

前端 未结 5 1100
旧时难觅i
旧时难觅i 2020-12-13 17:05

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

5条回答
  •  有刺的猬
    2020-12-13 18:11

    I agree to the point that programming with streams is nice and easier for some scenarios but when we're losing out on performance, why do we need to use them?

    Performance is rarely an issue. It would be usual for 10% of your streams would need to be rewritten as loops to get the performance you need.

    Is there something I'm missing out on?

    Using parallelStream() is much easier using streams and possibly more efficient as it's hard to write efficient concurrent code.

    Which is the scenario in which streams perform equal to loops? Is it only in the case where your function defined takes a lot of time, resulting in a negligible loop performance?

    Your benchmark is flawed in the sense that the code hasn't been compiled when it starts. I would do the whole test in a loop as JMH does, or I would use JMH.

    In none of the scenario's I could see streams taking advantage of branch-prediction

    Branch prediction is a CPU feature not a JVM or streams feature.

提交回复
热议问题