Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Parallel stream performs faster than serial stream  RSS feed

 
Greenhorn
Posts: 21
2
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 38564
654
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thank you very much Brandon Lewis! Your examples were exactly what I was looking for.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!