Contiguous vs. Strided Access: Up to 100x Speed Gap

Sequential, contiguous memory access can be 10-100 times faster than strided or random access. This is due to high cache hit rates (e.g., ~87.5% for sequential) and efficient hardware prefetching, which avoids costly cache misses that can stall the CPU for hundreds of cycles.

The top 3

  1. CPU Cache Lines: The Contiguous Access Advantage: Modern CPUs fetch data in 64-byte "cache lines." Contiguous memory access fully utilizes these lines by maximizing spatial locality, leading to ~100-300x faster L1 cache access compared to main memory. Strided access, however, often fetches an entire cache line but uses only a fraction of it, resulting in frequent cache misses and wasted bandwidth.
  2. SIMD Instructions: Contiguous Data's Parallel Power: Single Instruction, Multiple Data (SIMD) units on modern CPUs process multiple data elements simultaneously. This vectorization is most effective with contiguous data (stride 1), allowing operations to be 10-100 times faster by reducing instruction overhead and improving cache locality, a benefit largely lost with strided accesses.
  3. AoS vs. SoA: Data Layout's Performance Impact: Struct-of-Arrays (SoA) data layouts often outperform Array-of-Structs (AoS) by organizing fields contiguously in memory, maximizing cache utilization and enabling more efficient SIMD vectorization for processing specific attributes across many objects.

Sources

Open the full topic