• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parallel stream performs faster than serial stream

 
Greenhorn
Posts: 21
2
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I have a question regarding the time between serial streams and parallel streams. In the following example, the serial stream processes the results faster than my parallel stream. Can anyone explain this behaviour please? Or point to me what I am doing wrong?



Thank you for the help!
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helene,
The IO cost of printing the value takes far longer than the CPU cost. The parallel version has the additional cost of parallelizing it. But since CPU isn't the primary resource, it doesn't provide a benefit.

Note that this code doesn't do what you think it does. You are creating a stream of one element; the number 13999999, And you are timing the looping through of creating those tiny streams. This isn't right. You really want to create one larger stream and start measuring time after it is created. Also, testing with a lambda that doesn't print will show you a different scenario.
 
Greenhorn
Posts: 5
2
  • Likes 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Helene,
You really want to create one larger stream and start measuring time after it is created. Also, testing with a lambda that doesn't print will show you a different scenario.



I think this might be more along the lines of the proof you're looking for, Helene.  Also note that I removed the IO operation as Jeanne suggested; even with a single System.out.print("") statement in each of the lambdas, the run times increased dramatically (see my comments for the actual numbers).

I think it's fun to look at the performance on these different streams given their operations (with and without IO) and their sizes.  For one million records without IO, processing the serial stream was much faster.  However, one hundred million records without IO were processed much faster in parallel than they were in a serial manner.  Your mileage will vary of course, but I found this to be an interesting baseline for serial/parallel streaming.  I hope this helps!

 
Helene Shaikh
Greenhorn
Posts: 21
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Brandon Lewis! Your examples were exactly what I was looking for.
reply
    Bookmark Topic Watch Topic
  • New Topic