How does cache locality affect the performance of C++ code?
Last updated
Cache misses to RAM can cost ~100x an L1 hit, so memory access patterns often matter more than instruction count.
Spatial locality (contiguous access reusing 64-byte cache lines) and temporal locality (reusing recent data) maximize cache hits.
Prefer
std::vectoroverstd::list, consider structure-of-arrays, and iterate arrays in memory (row-major) order.