How does cache locality affect the performance of C++ code?

Last updated 21 June 2026

  • 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::vector over std::list, consider structure-of-arrays, and iterate arrays in memory (row-major) order.

  • https://dev.to/hamzahassanain0/what-every-programmer-should-know-about-memory-part-4-4bh5
  • https://www.geeksforgeeks.org/dsa/why-arrays-have-better-cache-locality-than-linked-list/
  • https://dev.to/paradane/understanding-time-complexity-variations-in-similar-c-code-405a
All C++ interview questions