user3668129
user3668129

Reputation: 4810

Streams in parallel & number of cores

I'm running my app on I7 with 4 cores. I have about 10 Threads which runs in the background. in addition to the 10 threads, I have an algorithm which I can rewrite it with parallelStream, but I'm not sure it will give me better performance.(The app is not deterministic, so It will not be accurate to compare the performance between the 2 ways (with or without parallel).

if I have 4 cores, and I already have 10 threads, is it right to write the additional algorithm in parallel ?

Upvotes: 3

Views: 1826

Answers (1)

edharned
edharned

Reputation: 1904

Doug Lea has a tentative outline of when to use parallel in streams. You might want to read what he has to say here

He hits on some of the questions. BUT, it all comes down to number of processors. If you don't have the horsepower then you are not going to fly. Below is taken from documentation for a Data Parallelism product I maintain:

After all the huffing and puffing, scientific calculations and time-consuming testing, if you don't have sufficient processors to split the work into, and sufficient processors to handle the load, then parallelizing is not going to knock your socks off. Parallelizing may make processing slower:

If you have P processors and 8(P) concurrent requests, then using one thread per request is often more efficient for throughput. The break-even point for decomposition is somewhere just greater than 8(P), depending on the application. The logic here is simple. If you have P processors available and you split your work accordingly but there are hundreds of other tasks ahead of you, then what's the point of splitting? It may be faster to process each request sequentially.

Using a dual-core machine as a parallel engine is not going to work well. Quad-core is better but still somewhat lacking. Be careful when looking at the number of threads reported by the operating system. Many machines today use Simultaneous multithreading (SMT.) While SMT works well for general-purpose computing it often hurts performance for compute-intensive functions when all the hardware threads are participating. Using the Java Runtime.getRuntime().availableProcessors() does not always return the number of hardware threads. And specifying the number of threads for a session may have no bearing on the physical environment.

Upvotes: 3

Related Questions