1

What could be possible reasons when Java Parallel Stream is slower than sequential streams.

I understand in parallel streams there could be more context switching, but that was the idea behind having fork join internally which takes care of number of available cores in machine and distribute tasks.

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
Rohit Sachan
  • 1,178
  • 1
  • 8
  • 16
  • Creating threads, splitting the work among them and merging the results does not come for free. So sometimes, depending on the operations you are performing and on the data set, it may be faster to use a sequential execution. Of course, a proper benchmark will tell you which approach is faster. – Alexis C. Dec 16 '15 at 14:57
  • It's heavily depends on stream operations and whether the stream is ordered. Adding `limit()`, `skip()` or `distinct()` drastically reduces the parallel performance, often making it slower than sequential. Also `forEachOrdered()` terminal operation will likely to reduce the performance. – Tagir Valeev Dec 16 '15 at 16:25

0 Answers0