Home > Backend Development > C++ > body text

Why is iterating through std::vector faster than std::array in my benchmark?

Patricia Arquette
Release: 2024-11-02 03:19:02
Original
182 people have browsed it

Why is iterating through std::vector faster than std::array in my benchmark?

Why Iterating Through std::vector is Faster than Iterating Through std::array

In a previous benchmark, it was assumed that iterating through std::array would be faster than iterating through std::vector. However, subsequent revisions of the benchmark revealed that std::vector was actually the faster option. This article examines the reasons behind this unexpected finding.

Benchmark Improvements

The improved benchmark addressed the following flaws in the original implementation:

  • Ensuring the result of the loop is used to prevent optimization omissions.
  • Utilizing the -O3 flag for speed enhancements.
  • Employing std::chrono to isolate the measurement to the for loop only.

Benchmark Results

The resulting benchmark timings demonstrated that, contrary to the earlier assumption:

  • std::array: Execution time = 99.554109 milliseconds
  • std::vector: Execution time = 30.734491 milliseconds

Analysis

The observed difference in performance stems from the memory management differences between std::array and std::vector. In the case of std::array, the global scope array is initially stored in the .bss section of the executable, which resides in non-paged memory. Consequently, when the std::array is accessed for the first time, the memory pages are faulted in, resulting in the performance overhead.

In contrast, std::vector allocates and zero-fills its memory pages upon creation. This ensures that the pages are already present in memory when the loop iterates over the vector.

To illustrate this concept, a line of code has been added to the beginning of the main function to bring the memory pages of std::array into the address space:

<code class="cpp">std::fill_n(v.data(), n, 1);</code>
Copy after login

By pre-faulting the pages, the execution time of std::array is reduced to match that of std::vector.

Conclusion

The performance difference between iterating through std::array and std::vector arises from the memory management mechanisms. Pre-faulting the std::array pages or running the program on an operating system that supports mlock can improve the performance of std::array to match that of std::vector.

The above is the detailed content of Why is iterating through std::vector faster than std::array in my benchmark?. 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!