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:
Benchmark Results
The resulting benchmark timings demonstrated that, contrary to the earlier assumption:
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>
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!