Home > Backend Development > C++ > body text

Why is Iterating through `std::vector` Faster than `std::array` in This Benchmark?

Linda Hamilton
Release: 2024-11-01 14:50:02
Original
738 people have browsed it

Why is Iterating through `std::vector` Faster than `std::array` in This Benchmark?

The Perplexity of Iteration Performance: std::array vs. std::vector

In a previous inquiry, it was discovered that iterating through an std::array was noticeably faster than iterating through an std::vector. To rectify these flawed observations, a new benchmark was devised, revealing a startling reversal: iterating through std::vector was now significantly faster.

To avoid potential optimizations, the result of the loop was included, and the -O3 flag was enabled. Additionally, std::chrono was used for precise timing, isolating the execution of the loop.

The results were striking:

std::array:

$ ./a.out
result: 0
time: 99.554109
Copy after login

std::vector:

$ g++ arrVsVec.cpp -O3
$ ./a.out
result: 0
time: 30.734491
Copy after login

Unraveling the Mystery

Upon examining the disassembly in Godbolt, it became evident that the performance discrepancy stemmed from memory page residency within the process address space. While the std::vector had already been allocated and zero-filled, the std::array was stored in the global .bss section of the executable and its pages had not been paged in.

Resolving the Issue

To resolve this discrepancy, it is necessary to bring the std::array pages into the address space. This can be achieved by adding the following line at the beginning of the main function:

std::fill_n(v.data(), n, 1); // included in <algorithm>
Copy after login

or alternatively, on Linux, using mlock():

mlock(v.data(), v.size() * sizeof(v[0]));
Copy after login

Conclusion

This benchmark highlights the importance of page residency in the performance of iteration. By ensuring that the pages of both std::array and std::vector are present in the address space, the iteration performance can be significantly improved.

The above is the detailed content of Why is Iterating through `std::vector` Faster than `std::array` in This 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!