The CPU in every day life
Jan 22, 2024 - ⧖ 4 minA CPU is a lot like a bartender. You can think of the queue of work a CPU does as like a queue of patrons. The bartender takes patron's money and makes drinks. A CPU takes instructions and performs them for a computer program.
Instructions
A CPU has a set of instructions it knows how to do. We can imagine this as like recipes for our bartender. There are two types of instructions, complex (CISC) and simple (RISC). For complex, each drink might be its own instruction the bartender memorizes separately. For simple, each drink is an amalgamation of instructions the bartender knows. So gin and tonic might be:
Gin and Tonic
Simple = pour_gin, pour_tonic and drop_lime
Complex = make_gin_and_tonic
We can try to grasp the intuition that for simple we have fewer instructions but more of them for each drink. For complex, we have one instruction that covers one task.
If we build from our prediction concept earlier of trying to only do the work of getting the gin bottle only once for a queue of patrons. We would be able to see that simple would let us pour the gin for a gimlet and a gin and tonic at the same time. But complex we would have to get the gin bottle twice since the instructions don’t overlap in the same way.
Gin and Tonic
Simple = pour_gin, pour_tonic and drop_lime
Complex = make_gin_and_tonic
Gimlet
Simple = pour_gin and pour_lime_juice
Complex = make_gimlet
Now the downside of simple is that our bartender has a huge list of instructions for all the drinks they are making in their memory. Whereas with complex there’s just one for each drink and if the bartender just pulls beers all night and it’s one instruction in complex and two in simple then we have wasted space in the bartender’s memory.
Out of Order
A smart bartender would also pull people out of the queue in a different order than they arrived to make sure that fast orders can go through like opening bottles for a recent arrival while they wait for the coffee pot to brew for another longer waiting customer. CPU’s do the same thing with instructions so that the very fast simple instructions don’t get blocked behind slow ones.
Prediction
A really great bartender can learn what customers might order and make it in advance. To save trips to get a bottle from a shelf or the fruit from beneath the bar. If they see someone in the queue that will likely get another gin and tonic they might make two the next time they pour one for a different customer. That way when the other customer reaches the bar their drink is ready.
CPU’s do this for branches in code. Where a branch is any if this then that statement. They learn and guess. The same way as our excellent bartender looking at their queue of customers.
Switching Programs
Now every time the bartender switches to a new bar or a new set of patrons arrive they have to learn all over again. This is just like switching the program running on a CPU. The new patrons or program can’t always be predicted with what we learned from the last one. So we throw away what we learned and start again.
Summary
Our smart bartender and modern CPU use simple instructions, out of order work and prediction. With prediction we are guessing what people might want in our queue of customers. With out of order we are serving people with the goal of minimizing the time any one customer waits. With simple instructions we can maximize our ability to predict and do less work. Finally, our bartender or CPU also needs a little time for new bars or programs to learn to predict correctly.