How does CPU cache works simplified | Tech-Knowledge
Updated: Jan 3, 2021
CPU cache is the fastest memory in the processor. It basically decrease the time taken to access data by the processor. To understand how does CPU cache works first we'll go through a story which makes it simple to understand the working principle of the cache.
I came across this story long back when I was going through the same query. This story helps me a lot to understand the relationship of cache memory and how does CPU cache works.
Imagine a clerk at one of our 'really efficient' govt. offices. He's sitting there at his desk with all sorts of papers lying around. There is a drawer for the desk where he's stashed away a larger bundle of papers and you can see a large cupboard behind him where he's got tons of papers-with God only knows what-dating back to before independence!
You go to him saying, ''Hey buddy, could you get me paper ABC?'' . What does he reply? He says, ''Oh yeah, ABC right? I think I just saw that here on the table!''. He searches through the 64 papers lying on the desk and picks up a paper, squints at it, smacks his forehead and says, ''Arey! It was ABD! Not ABC!''. "Wait but I really think I saw it a couple of days back, it must be here itself". He then bends down opens the drawer of the desk and takes out a bundle of about 512 papers and puts it on the table and proceeds scanning through them. After a couple of minutes he says, ''Sir, it's not here also. Please come tomorrow". But you say, "Please, it's for an urgent matter". He looks at you with displeasure, looks at the clock, mumbles something, and reluctantly gets up and goes to the cupboard. You can clearly see there's about 8000 papers in there! He goes through them and pulls out a document, going into a coughing and sneezing fit, because of all the dust it spins up, and hands you your paper. He says, ''If that wasn't there, we would have had to go to the room in the back and search through about 2 million papers!". You look at it, thank God and get out of there before the clerk has time to think about 'monetary transactions'.
Evaluation of the story:
Here, the clerk is the processor. The space on top of the table that can hold those 64(kB) papers and is closest to him and can be searched fastest is the L1 cache. The space in his drawer that can hold a larger number of papers (512kB) and takes a bit more time to search is the L2 cache. The cupboard behind him that has a still larger storage capacity (8000kB) is the L3 cache. The storage room with the largest capacity (2GB here) is the RAM.
L1, L2 and L3 caches are different memory pools similar to the RAM in a computer. They were built in to decrease the time taken to access data by the processor. This time taken is called latency. The architecture they are built with also differs considerably. For example the L1 cache is built using larger transistors and wider metal tracks, trading off space and power for speed. The higher level caches are more tightly packed and use smaller transistors.
There are other complexities to the story also like the fact that other clerks (processors) can open and search (has access to) the cupboard (L3 cache).
The access times of data in these memories are usually of the order of nanoseconds and do not really make a difference to the average user. But for scientists/programmers that develop/use programs that process a lot of data, it matters. They make a huge difference of the order of days or months even!
In other way,
L1 and L2 are levels of cache memory in a computer. If the computer processor can find the data it needs for its next operation in cache memory, it will save time compared to having to get it from random access memory. L1 is "level-1" cache memory, usually built onto the microprocessor chip itself. For example, the Intel MMX microprocessor comes with 32 thousand bytes of L1.
L2 (that is, level-2) cache memory is on a separate chip (possibly on an expansion card) that can be accessed more quickly than the larger "main" memory. A popular L2 cache memory size is 1,024 kilobytes (one megabyte).
I hope it become easy to remember. Is not..!!