Card 08/ 10
All 10 cards
GotchaDifficulty: Advanced1 min
Why parallel() Made It Slower
One word added to a pipeline that was taking too long. The machine has eight cores, the work is now spread across all of them, and the whole thing takes longer than it did.
long total = invoices.parallelStream()
.mapToLong(Invoice::pence)
.sum();Going parallel costs something before it saves anything: the source has to be split, tasks have to be handed to a thread pool, and the partial results have to be combined. On a few thousand cheap operations that overhead is most of the time.
| Needs | Why |
|---|---|
| A large source | Thousands of elements at least, usually many more |
| Expensive work per element | Cheap work is dominated by the splitting |
| A source that splits evenly | An ArrayList does; a LinkedList and a file do not |
| No shared mutable state | Otherwise the result is wrong rather than slow |
Every parallel stream also shares one pool by default, so a slow parallel pipeline in one part of an application delays every other one. Measure before adding it, and measure again after.
- Java
- Java Streams
- Performance