# What Programmers Should Know About Memory
AI-readable version of this post.
Path: /systems/what-programmers-should-know-about-memory
Category: Systems
Date: 2007-11-21
Author: Performance
Reading time: 1 min
Description: Why cache behavior dominates many real-world performance outcomes.
## Memory hierarchy

Registers, caches, RAM, and storage have very different latency and bandwidth. Algorithms that ignore locality pay large runtime costs.

## Locality

- Temporal locality: reuse data soon
- Spatial locality: access nearby addresses
- Sequential access helps prefetchers

## Example

```tsx
// cache-friendly
for (let r = 0; r < rows; r++)
  for (let c = 0; c < cols; c++) sum += matrix[r][c];

// cache-hostile
for (let c = 0; c < cols; c++)
  for (let r = 0; r < rows; r++) sum += matrix[r][c];
```