Thursday, September 24, 2015

Java 8: Is parallel stream faster?

Here is my testing code

Here is the result

2.1 -------------------
total words 27343
count: 16
6 ms
count: 16
112 ms
count: 16
3 ms

 

The first parallel stream is much slower. If we switch the implementation 2 with implementation 3. Still the second one is the slowest one, which means the initializing of the parallel stream take some cost.

We should not use parallel stream until we really see performance issue. I can imagine two reasons

1. real big data, currently my list size is only 27343, it seems it is not worth to make it parallel.

2. the lambda is a high cost method, then we may need it parallel to use the power of multiple cpu.

No comments:

Post a Comment