Home > Java > javaTutorial > body text

Is Java 8\'s Streams API faster than traditional Collections in performance-critical scenarios?

Mary-Kate Olsen
Release: 2024-11-03 10:22:29
Original
335 people have browsed it

Is Java 8's Streams API faster than traditional Collections in performance-critical scenarios?

Java 8: Streams vs Collections Performance Analysis

Evaluating the performance of the recently introduced Streams API in Java 8 compared to the traditional Collections approach is a crucial aspect for developers. To provide insights, an initial benchmark was conducted, which raised questions about the comparative efficacy of these two methods.

Benchmark Setup and Findings

The benchmark involved filtering a sizable list of integers and calculating the square root of even numbers, storing the results in a list of Double. The code snippet below illustrates the implementation:

<code class="java">    // Source list initialization
    List<Integer> sourceList = new ArrayList<>();
    for (int i = 1; i < 1000000; i++) {
        sourceList.add(i);
    }

    // Collections approach
    List<Double> resultCollection = new LinkedList<>();
    long startTimeCollection = System.nanoTime();
    // Iterate through the list and perform calculations
    for (Integer i : sourceList) {
        if (i % 2 == 0) {
            resultCollection.add(Math.sqrt(i));
        }
    }
    long elapsedTimeCollection = System.nanoTime() - startTimeCollection;


    // Stream approach
    Stream<Integer> stream = sourceList.stream();
    long startTimeStream = System.nanoTime();
    // Filter even numbers and calculate square roots
    resultStream = stream.filter(i -> i % 2 == 0)
                        .map(i -> Math.sqrt(i))
                        .collect(Collectors.toList());
    long elapsedTimeStream = System.nanoTime() - startTimeStream;

    // Parallel stream approach
    stream = sourceList.stream().parallel();
    long startTimeParallelStream = System.nanoTime();
    resultParallelStream = stream.filter(i -> i % 2 == 0)
                                .map(i -> Math.sqrt(i))
                                .collect(Collectors.toList());
    long elapsedTimeParallelStream = System.nanoTime() - startTimeParallelStream;</code>
Copy after login

The results on a dual-core machine revealed that:

  • Collections approach performed noticeably faster, taking approximately 0.094 seconds.
  • Stream approach showed a slower performance, requiring about 0.201 seconds.
  • Parallel stream approach exhibited similar performance to the stream approach, completing in 0.357 seconds.

Analysis of Benchmark Results

Based on these initial findings, it was initially concluded that streams were slower than collections, with even parallelism failing to improve performance. However, the benchmark methodology employed raised concerns about potential flaws.

Improved Performance Verification

To address these concerns, the benchmark was revised with the following refinements:

  • Execution: The benchmark was run 1,000 times after JVM warmup to stabilize performance.
  • Profiling: JMH (Java Microbenchmarking Harness) was used to execute the benchmark accurately and collect performance data.

Updated Benchmark Results

The revised benchmark yielded the following results:

  • Collections approach: Average time of 0.207 seconds
  • Stream approach: Average time of 0.098 seconds
  • Parallel stream approach: Average time of 0.168 seconds

In this revised benchmark, streams outperformed collections, contrary to the initial findings. The faster execution time of the stream approach can be attributed to JIT optimizations and improved code generation by the compiler.

Conclusion

Based on these updated findings, it can be concluded that streams in Java 8 offer both coding convenience and performance enhancements when compared to traditional collections. While streams are not always superior, their use can significantly simplify code and improve efficiency in many scenarios.

Best Practices

To leverage the benefits of streams effectively, consider the following best practices:

  • Use inline lambda expressions for brevity and efficiency.
  • Avoid unnecessary intermediate Lists and focus on using a target collection directly.
  • Explore the parallel stream capabilities to optimize performance in certain situations.

The above is the detailed content of Is Java 8\'s Streams API faster than traditional Collections in performance-critical scenarios?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!