The Memory Hierarchy

Levels

Crude estimate as of 2022 of highly variable cache size and performance. Depends heavily on purpose of processor (low power laptop vs high performance computing / gaming server)

            SWAP        Gibibytes   milliseconds
            DRAM        Gibibytes   ~50+ nanoseconds
            L3 cache    ~100 MiB    ~20-40 ns
            L2 cache    ~10 MiB     ~5-10 ns
            L1 cache    ~1 MiB      ~1 ns
            

The less memory a program uses, the more likely most of its data will fit in faster levels of memory, and the lower the average memory access time (AMAT).

Virtual Memory

Average disk access time for a random block is a few milliseconds, write buffering, disk caching and predictive reads reduce this.

Cache

Multiple levels, hit ratio.

Restricting memory access to a smaller range improves hit ratio and can have a dramatic impact on program speed.

Filling a 64.00 KiB array 16384 times 1 bytes at a time...
   64.00 KiB array         1.00 B blocks       653.00 ms      1568.15 MiB/s

Filling a 64.00 KiB array 16384 times 2 bytes at a time...
   64.00 KiB array         2.00 B blocks       263.00 ms      3893.54 MiB/s

Filling a 64.00 KiB array 16384 times 4 bytes at a time...
   64.00 KiB array         4.00 B blocks       147.00 ms      6965.99 MiB/s

Filling a 64.00 KiB array 16384 times 8 bytes at a time...
   64.00 KiB array         8.00 B blocks        89.00 ms     11505.62 MiB/s

Testing large array, low cache hit-ratio...
Filling a 512.00 MiB array 5 times 1 bytes at a time...
  512.00 MiB array         1.00 B blocks      1905.00 ms      1343.83 MiB/s

Filling a 512.00 MiB array 5 times 2 bytes at a time...
  512.00 MiB array         2.00 B blocks       681.00 ms      3759.18 MiB/s

Filling a 512.00 MiB array 5 times 4 bytes at a time...
  512.00 MiB array         4.00 B blocks       525.00 ms      4876.19 MiB/s

Filling a 512.00 MiB array 5 times 8 bytes at a time...
  512.00 MiB array         8.00 B blocks       509.00 ms      5029.47 MiB/s
            
Memory Access Time

Example: 30ns DRAM, 2ns cache. Hit ratio 0.9, avg access time is 0.9 * 2 + 0.1 * 30 = 4.8 ns. Hit ration 0.5, avg access time is 0.5 * 2 + 0.5 * 30 = 16 ns.

With 1% VM swapping: 4ms disk (1ms avg access time), 30ns DRAM, 2ns cache. 0.01 * 1,000,000ns + 0.69 * 30ns + 0.3 * 2ns = 10,021.3ns