Cache hit rate vs size and associativity: experiment bench
Cache hit rate vs size and associativity
Run a repeating array scan through a small cache and compare direct-mapped with 2-way and 4-way set-associative caches of the same size.
- 1. Ask
- 2. Hypothesise
- 3. Set up
- 4. Run
- 5. Record
- 6. Change one variable
- 7. Compare
- 8. Interpret
- 9. Limitations & save
If I change …, then … will …, because …
3 · Set up the parameters
The cache holds 2^value blocks: 0 → 1 line, 6 → 64 lines.
0 = direct-mapped, 1 = 2-way, 2 = 4-way, 3 = 8-way (never more ways than lines).
Bigger blocks bring neighbouring bytes in on each miss.
Bytes in the array being scanned.
Repeated passes reward a cache big enough to keep the array.
2 more controls appear at higher levels; until then they stay at the default value.
The seed fixes any random noise. Same parameters + same seed = exactly the same result, every time, on every computer.
6 · Change one variable
Your first run is the baseline. After it, change one thing at a time.
7 · Compare on a graph
5 · Run log
No runs yet.
Equations
block = floor(address / block size); set = block mod sets; sets = lines / ways. Least-recently-used replacement inside a set.
Assumptions (always true in this model)
- Reads only: no writes, so no write policy.
- One cache level, least-recently-used replacement, no prefetching.
- The trace is a deterministic array scan; real programs mix many patterns.
- Timing is not modelled: only hits and misses are counted.
Text description of what the chart shows
A line chart of cache hit rate against cache size, from 1 to 64 lines, for direct-mapped, 2-way and 4-way caches. Hit rate rises once the cache can hold the whole array. When a second array maps onto the same sets, the direct-mapped line stays at zero while the associative caches recover.
Did the result support your hypothesis? Use the numbers from the comparison table and link them to an equation.
Which assumptions could change your answer in the real world?
Export and save
Exports use the ticked runs (or every run if none are ticked). Your draft saves automatically in this browser only. The Journal is private to you.
Valid parameter ranges
| Parameter | Range | Default |
|---|---|---|
| Cache lines (log₂) | 0 to 6 | 3 |
| Associativity (log₂ ways) | 0 to 3 | 0 |
| Block size (log₂ bytes) | 0 to 4 | 2 |
| Array length | 4 to 256 B | 64 |
| Passes over the array | 1 to 8 | 2 |
| Stride | 1 to 16 B | 1 |
| Second array in the same sets (0/1) | 0 to 1 | 0 |
Independent reference checks
array fits, direct-mapped
Hand: 16 bytes = 4 blocks of 4; first pass misses once per block, second pass all hits: 28/32.
two arrays thrash a direct-mapped cache
Hand: A[i] and B[i] = A[i] + 1024 map to the same set and evict each other on every read.
same size, 2-way removes the conflict
Hand: 8 blocks over 4 sets of 2 ways all fit: 8 compulsory misses, then hits: 56/64.
2-way but too small: capacity misses
Hand-traced LRU: 8 blocks compete for 4 lines; each block misses once per pass: 16 misses, 48/64.
stride equal to block size
Hand: every read lands in a new 4-byte block, one pass: 16 compulsory misses.
Checked automatically on every change to the code. Educational model, not engineering-grade.