Understanding CPU Cache for C++
- Éloïse Hamadi
- 12 févr.
- 1 min de lecture
Dernière mise à jour : 17 févr.

Introduction
Modern CPU are lightning-fast, but memory access can slow everything down. Every time the CPU needs data from RAM, it has to wait like a car at a red light!
To speed things up, processors use a cache: a small, high-speed memory that holds frequently used data. When used effectively, cache can make your C++ programs run much faster.
How CPU Cache works ?
Think of CPU as quick-access toolbox for your processor. Instead of constantly going to the storage room (RAM) for tools, the CPU keeps frequently used data.
has the most used shoes closer than the ones rarely use.
There are thee levels of cache:
L1 Cache - Super fast, but very small. Located ihnside the CPU core.
L2 Cache - Bigger, but little bit slower
L3 Cache - Larger but slower Shared across all CPU cores
What happens when you access Data ?
The CPU first checks L1 cache. If the data is there cache hit!
If not, it looks in L2 cache.
Still not found? It checks L3 cache.
If the data not in any cache (cache miss), the CPU checks in the RAM which is slower in transfering data.
Keep reduce cache misses and keep important data as close to the CPU.
Comments