Menu

Benchmarking Java8 stream and parallelStream against old school fashion

JDK8 brings in the game new stuff like streams and PARALLEL streams. The question is how fast can they be compared to old school manner to achieve the same task ? Answers…

My test case does not intend to be complete, it’s a good enough test case i defined for what i had in mind. Anyway i think it’s good enough too to extract a significant benchmark and draw some conclusions.

Test Case

An Integer list is populated with 10 Millions or Random integer from MIN to MAX Integer, then we go over this same list for each of the 3 methods.

The same list is also use to look for the MAX with the old school get last from the ASC sorted list JDK7 way, max() for JDK8 stream and parallelStream fashions

I put Collector in the picture because by nature steams are empty (does not have content) so in the real world at the end we will need to collect stream to something we can manipulate such as List. This over-head deserves to be calculated (in my opinion)

It means Collectors operation on Parallel Steam are Slower than for a Single thread processing Stream: which is maybe surprising but totally explainable

Finding MAX value with Classical Stream is about 25 X faster

Finding MAX value with Parallel Stream is about 41 X faster

Bottom line Finding MAX with Parallel Stream is 1.6 X faster than with normal Stream

Streams are faster in deed and PARALLEL streams are EVEN FASTER but be aware of the overhead they bring regarding collect method. If you are not impacted by concurrency constraint maybe using your own map() function to process each item directly on the flight from the stream will be a better option (compared to collect them in a List that you will process later then)