Java Reference
In-Depth Information
Data size
There is a difference in the efficiency of the parallel speedup due to the size of the input
data. There's an overhead to decomposing the problem to be executed in parallel and
merging the results. This makes it worth doing only when there's enough data that execu-
tion of a streams pipeline takes a while. We explored this back in Parallel Stream Opera-
tions .
Source data structure
Each pipeline of operations operates on some initial data source; this is usually a collec-
tion. It's easier to split out subsections of different data sources, and this cost affects how
much parallel speedup you can get when executing your pipeline.
Packing
Primitives are faster to operate on than boxed values.
Number of cores
The extreme case here is that you have only a single core available to operate upon, so
it's not worth going parallel. Obviously, the more cores you have access to, the greater
your potential speedup is. In practice, what counts isn't just the number of cores allocated
to your machine; it's the number of cores that are available for your machine to use at
runtime. This means factors such as other processes executing simultaneously or thread
affinity (forcing threads to execute on certain cores or CPUs) will affect performance.
Cost per element
Like data size, this is part of the battle between time spent executing in parallel and over-
head of decomposition and merging. The more time spent operating on each element in
the stream, the better performance you'll get from going parallel.
When using the parallel streams framework, it can be helpful to understand how problems
are decomposed and merged. This gives us a good insight into what is going on under the
hood without having to understand all the details of the framework.
Let's take a look at how a concrete example is decomposed and merged. Example 6-6 shows
some code that performs parallel integer addition.
Example 6-6. Parallel integer addition
private
private int
int addIntegers ( List < Integer > values ) {
return
return values . parallelStream ()
Search WWH ::




Custom Search