File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Features new in Java 8 and the fly likes New stream() and parallelStream() methods in the Collection interface. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 8
Bookmark "New stream() and parallelStream() methods in the Collection interface." Watch "New stream() and parallelStream() methods in the Collection interface." New topic
Author

New stream() and parallelStream() methods in the Collection interface.

Renato Perini
Greenhorn

Joined: Apr 11, 2012
Posts: 4

Hello guys.
I'm experimenting with the new collection framework, using the new Lambda Expressions.
I wrote a simple class defining a point, as a test:



Next, some code to excercise an ArrayList:



Finally, the results from the execution:



I'm using JDK8 Early Access (Build 111) for Windows x64 with a fairly recent processor (Intel Core i7 2600 3.4Ghz, 4 Cores, 8 threads [Hyperthreading ON]).
Someone knows what the problem can be? The new lambda based iteration mechanism took almost twice the time to iterate a collection!!!

/ Renato Perini


You can't live with women. You can't live without women.
Fuzzy Logic Example.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
That's such a small time, it's unlikely the test is very meaningful. The test is too short for the JVM to properly optimize the code. Try repeating the loop a bunch of times to see what happens over longer times. Also try reversing the order that you run the two tests. Does it make a difference?

If the difference does hold up, there are a few possibilities:

How many processors does your machine have? If it's only one, there is no reason to expect any improvement here from parallelStream().

There is probably a little extra overhead in setting up the Stream objects in the first place, and in setting up parallel threads. If you don't give them a big enough job to do, it's not worthwhile.

This is a new feature, and it's possible there are flaws in the implementation. Don't be too quick to assume this. But it's possible.
Andrew Samii
Greenhorn

Joined: Mar 01, 2011
Posts: 5
That's strange - can you try using the non parallelStream and see what difference that makes? I'm downloading the beta now but it won't be set up for a while.

Is parallelStream changing the underlying structure of the ArrayList at all? I would say to really do a good test, these would have to be run in separate files, but you could also mimic that with a fresh ten million elements - it's possible you tripped a code branch heavily setting those up, corrected it iterating through them on parallelStream()'s dime, and gave the benefit to Iterator. I'm going to play around with it and see.
Renato Perini
Greenhorn

Joined: Apr 11, 2012
Posts: 4

Mike Simmons wrote:That's such a small time, it's unlikely the test is very meaningful. The test is too short for the JVM to properly optimize the code. Try repeating the loop a bunch of times to see what happens over longer times. Also try reversing the order that you run the two tests. Does it make a difference?


I'll try, but it's unlikely. 10 million items in a list is way more than a real use case might be.


If the difference does hold up, there are a few possibilities:

How many processors does your machine have? If it's only one, there is no reason to expect any improvement here from parallelStream().


4 cores, 8 execution threads. I generally obtain huge improvements in parallelism over single thread execution in many applications.


There is probably a little extra overhead in setting up the Stream objects in the first place, and in setting up parallel threads. If you don't give them a big enough job to do, it's not worthwhile.


Mmmm. True, I haven't considered this. I'll do a refined test and report some improvements / regressions.

/Renato Perini
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 320
    
    2
Could you also try to do some more lengthy calculation than a translation? For instance the length of a point
(with sqrt)? And could you calculate the average length then, both by using the iterator and by using the
average() method?

Thanks in advance!

Greetings,
Piet
Andrew Samii
Greenhorn

Joined: Mar 01, 2011
Posts: 5
I modified the code to do busy work, as per Piet's idea.

Piet, I didn't fully understand what you were going for with the average - would that just run after the first Stream/Iterator executed and completed? If that's the case, then the operation to retrieve the average from each object would be even simpler than the transpose, just a get, and wouldn't tell us anything that I can see, but I probably misunderstood.

This was one of the iterations of my code, as I explain further down, I tried it with 0,1,10 and 20 square root functions per Point to see how increased execution helped.


EDIT: I just realized I missed the Math.pows() in there, went back and tried it, and it really changes the results. Adding more to the bottom.

I also added a .stream() to test out the capabilities of the non parallel version and see how it stacked up.

NO SQUARE ROOTS
List creation: 0 seconds.
Point translation (Java 7): 154 milliseconds.
Point translation (Java 8 parallel): 247 milliseconds.
Point translation (Java 8 non-parallel): 161 milliseconds.

Stream is just a little slower than Iteration, but it is consistently lacking that 10-15ms. Now I decided to put in some busy work, so I added a square root -

ONE SQUARE ROOT
List creation: 3 seconds.
Point translation (Java 7): 153 milliseconds.
Point translation (Java 8 parallel): 249 milliseconds.
Point translation (Java 8 non-parallel): 162 milliseconds.

The iterator actually went DOWN that time, so clearly one square root wasn't cutting it.

TEN SQUARE ROOTS
List creation: 10 seconds.
Point translation (Java 7): 612 milliseconds.
Point translation (Java 8 parallel): 525 milliseconds.
Point translation (Java 8 non-parallel): 606 milliseconds.

It looked like it was getting better, so I moved it up to twenty, which is as far as I can go without it taking forever creating all those objects -

TWENTY SQUARE ROOTS
List creation: 8 seconds.
Point translation (Java 7): 1108 milliseconds.
Point translation (Java 8 parallel): 824 milliseconds.
Point translation (Java 8 non-parallel): 1086 milliseconds.

I'm using an older intel quad here at work, so it's not the greatest of cpu's - but that's still four cores to thread against, so my thinking is that square roots are not the most parallel of activities. If anyone knows a better suited method, I can try to implement it.

EDIT: I think I found it. Exponents - which should have been in the original code, but I messed up. Changed the sqrt function to



1 Iteration:
List creation: 0 seconds.
Point translation (Java 7): 1836 milliseconds.
Point translation (Java 8 parallel): 771 milliseconds.
Point translation (Java 8 non-parallel): 1849 milliseconds.

10 Iteration:
List creation: 10 seconds.
Point translation (Java 7): 2048 milliseconds.
Point translation (Java 8 parallel): 706 milliseconds.
Point translation (Java 8 non-parallel): 2022 milliseconds.

It actually went DOWN in time for 10 exponents!)

20 Exponents
List creation: 7 seconds.
Point translation (Java 7): 2308 milliseconds.
Point translation (Java 8 parallel): 873 milliseconds.
Point translation (Java 8 non-parallel): 2295 milliseconds.

Removed square roots, just 30 pairs of exponents:
List creation: 11 seconds.
Point translation (Java 7): 2490 milliseconds.
Point translation (Java 8 parallel): 1107 milliseconds.
Point translation (Java 8 non-parallel): 2424 milliseconds.


So, I guess my takeaway is that for Parallel Stream to be worth it, there has to be a huge amount of calculations, or you have to really understand how your program scales to parallelism - you can't slap it on and expect some huge performance increase.
Renato Perini
Greenhorn

Joined: Apr 11, 2012
Posts: 4

So, in the end, parallelStream() is more adequate for more heavy computations than regular iterations with simple operations ...

/Renato Perini
Jamie Coleshill
Greenhorn

Joined: Oct 18, 2013
Posts: 8

There was an article in the recent Java Magazine that discusses Java 8, collections, Lambdas, and various methods for using them all together. Here is the link to that article if anyone is interested in reading it.

Java Magazine Sept/Oct 2013
Cay Horstmann
author
Ranch Hand

Joined: Nov 14, 2004
Posts: 114
    
  10
Also, try this:



It's much faster.

Cheers,

Cay


Author of Java 8 for the Really Impatient
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New stream() and parallelStream() methods in the Collection interface.
 
Similar Threads
Default Constructor ...
how to reverse an image?
Balls are not moving
Bouncing balls
How to make card game like rummy card game.