How to check parallel performance with Spliterators in Java

Suppose we are given a data structure that also implements the separator. What is the boilerplate code to test if parallel processing using this delimiter is actually better than serial processing?


source to share

2 answers

To test the delimiter itself, you can create a sequential and parallel stream and shrink it with a simple pruning operation with minimal overhead. For example:

public sequential(Blackhole bh) {
    bh.consume(, false).reduce((a, b) -> a));

public parallel(Blackhole bh) {
    bh.consume(, true).reduce((a, b) -> a));


It is usually more important to check if your parallel spliterator is working correctly for any sequence of calls trySplit/tryAdvance/forEachRemaining

. Before testing speed, it's a good idea to thoroughly test the correctness.

It is also probably a good idea to create a test that is closer to real life. Think about how your data structure would be used in production code and create a test that implements such a real example in parallel and sequentially. Such results will be more relevant to users of your data structure.



While Tagir Valeev's answer has already covered the basics, you should remember the following:

Parallel processing can be faster than two reasons

  • an explicit size of the data structure, limiting what a single core can handle due to memory bandwidth limitations.
  • intermediate / collector stages in the pipeline are expensive enough that their parallel execution will amortize the parallelization overhead

The latter can already pay off with much smaller data structures and perhaps even inefficient spliterator behavior.

Ideally, you should check both, because asymptotically good performance doesn't mean it performs well at the near end of the curve.



All Articles