Performance Evaluation of Java Stream API: Serial vs Parallel Execution
This article presents a series of Java Stream API performance experiments—comparing serial and parallel streams on primitive, object, and reduction tasks—showing that while serial streams are slower than traditional loops for simple operations, parallel streams can significantly outperform both in multi‑core environments.
The article investigates the performance of Java Stream API, questioning whether its convenient syntax incurs a noticeable overhead compared to traditional loops.
All tests run the JVM in -server mode on a CentOS 6.7 server equipped with an Intel Xeon X5675 (6 cores, 12 threads), 96 GB RAM, and JDK 1.8.0_91. To reduce variability, the CMS garbage collector is forced with -XX:+UseConcMarkSweepGC -Xms10G -Xmx10G and JIT compilation is triggered early using -XX:CompileThreshold=10000. Parallel streams use the shared ForkJoinPool.commonPool(), with CPU affinity controlled via the Linux taskset command.
Experiment 1 – Primitive Iteration : Finding the minimum value in an int array. Results show serial streams are roughly twice as slow as a for‑loop, while parallel streams on all 12 cores outperform both.
Experiment 2 – Object Iteration : Finding the smallest string in a list. Serial streams are about 1.5× slower than loops, but parallel streams again surpass both serial streams and loops, especially as core count increases.
Experiment 3 – Complex Reduction : Summing transaction amounts per user from a list of <userName,price,timeStamp> tuples represented by an Order object. Parallel reduction is slower on a single core but improves markedly with more cores, ultimately beating serial and manual implementations.
Conclusion : For simple traversals, external loops are faster in single‑core scenarios, but parallel streams exploit multi‑core CPUs to deliver superior performance. For complex reductions, parallel streams also provide the best results on multi‑core systems, while serial streams remain comparable to manual code. The article recommends using external loops for trivial tasks, Stream API for complex logic, and parallel streams whenever multiple cores are available.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
