23.13 Java SE 8: Sequential vs. Parallel Streams

In Chapter 17, you learned about Java SE 8 lambdas and streams. We mentioned that streams are easy to parallelize, enabling programs to benefit from enhanced performance on multi-core systems. Using the timing capabilities introduced in Section 23.12, Fig. 23.29 demonstrates both sequential and parallel stream operations on a 10,000,000element array of random long values (created at line 17) to compare the performance.

 1   // StreamStatisticsComparison.java 2   // Comparing performance of sequential and parallel stream operations. 3   import java.time.Duration; 4   import java.time.Instant; 5   import java.util.Arrays; 6   import java.util.LongSummaryStatistics; ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.