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)

Computer hardware specs

Intel(R) Core(TM) i3-3217U CPU @ 1.80GHz, 2 Cores (4vCPU), 8 Gb of RAM on Ubuntu 14.04.4 LTS running Oracle JDK 1.8.0_91

Source Code

The source code is available on github https://github.com/zoumhussein/JDK8StreamsBenchmark

Some results

******** sort OLD School JDK 1.7 *******
duration :13920

******** sort STREAM NOT collected JDK 1.8 *******
duration :5

******** sort STREAM collected JDK 1.8 *******
duration :534

******** sort PARALLEL STREAM NOT collected JDK 1.8 *******
duration :0

******** sort PARALLEL STREAM collected JDK 1.8 *******
duration :1186

******** MAX OLD School JDK 1.7 *******
duration : already computed
2147483425

******** MAX PARALLEL STREAM collected JDK 1.8 *******
duration :335
2147483425

******** MAX STREAM collected JDK 1.8 *******
duration :534
2147483425

Conclusions

  1. Old school Sorting fashion nearly takes 14s which will be the baseline for comparison
  2. Classical Stream Sorting is more than 2700 X faster
  3. Parallel Stream Sorting is INSTANTANEOUS
  4. Classical Stream Sorting + Result collected in List about 26 X faster
  5. Parallel Stream Sorting + Result collected in List about 12 X faster
  6. Collect() operation (here at least) makes PARALLEL stream SLOWER than classical steam
  7. It means Collectors operation on Parallel Steam are Slower than for a Single thread processing Stream: which is maybe surprising but totally explainable
  8. Finding MAX value with Classical Stream is about 25 X faster
  9. Finding MAX value with Parallel Stream is about 41 X faster
  10. 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)

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s