Why keeping things in a mess is the most optimal algorithm
Originally published on Medium
Conventional wisdom is wrong
(by John Weinhardt on Unsplash)
If you’re after efficiency and productivity in your life, it’s time to ditch the conventional wisdom. Conventional wisdom is wrong.
I started on this journey a little over 3 months ago. Armed with a degree in computer science, practical experience, and great books, I’m applying what I learned to the real world. There’s so much wisdom here, problems solved in computer science, that are applicable to your life. Solutions you might have missed because you’re not in direct intersection with the field.
I’m not making up unfalsifiable claims from thin air. I’m backed by proofs in computer science. That is the first tenet of computer science: If you can’t prove it, it doesn’t work.
Let’s examine that pile of clothes building up in your room. How often do you pick up something to wear from this pile, compared to going through your closet? My pile is 3 pieces of clothing big — what I’ll wear today.
A concept in Computer science can help us understand this better: Caching. When you have a lot of data to access quickly, searching through the entire storage is inefficient. What you need is a storage mechanism closer to you: small, fast, having exactly what you need. The cache is this storage mechanism.
Then comes the question, what should I put in the cache?
In computer science, the term Clairvoyant Algorithm describes the perfect algorithm — which anticipates your needs and preloads the cache with what you need. The second best attempt by scientists is the LRU Algorithm, or Least Recently Used. It’s based on the idea that you’re less likely to need something you haven’t used in a long time, compared to something you used yesterday.
You’re less likely to need something you haven’t used in a long time, compared to something you used yesterday.
This can seem a bit counterintuitive.
The way I see it, the importance of anything goes down with time left unused. If you don’t use some data for a while, it’s not that important. The algorithm removes from the cache the file you haven’t used the longest, and inserts the file you just used at the top.
The proof that this works is here.
The next bit we need is cache hierarchies.
One cache makes things faster. How about three caches then? That’s exactly what scientists did with computers.
There’s the slow disk storage with all the data, your hard disk.
Then comes a faster, smaller cache, called L3 cache, or level 3 cache.
Then comes a faster, smaller cache, called L2 cache, or level 2 cache.
Then comes a faster, smaller cache, called L1 cache, or level 1 cache.
You start looking in the L1 cache, which is the smallest storage of the most important things you’ve been using. If you don’t find things there, you have a cache miss and you move to the larger L2 cache, and so on, until you find what you’re looking for.
Coming back to our pile of clothes, why is this mess better?
Do you see how this is a cache for the closet? Or let’s start at the basement storage. Clothes you haven’t worn in ages or have outgrown — they are in your hard disk storage — the basement / trunk.
Then comes the smaller closet. The L3 cache. Sure, you could go through the closet and choose what to wear everyday, if a new attire a day is your goal. I’m still all in for the efficiency, and this feels too slow and too time consuming. I need something faster, something smaller.
Then comes the pile on your chair. Smaller, limited selection. The L2 cache. Reasonably efficient. You can choose what you want to wear from the pile, and akin to the LRU Algorithm, what you’ve used the last goes on top.
I’ve taken this one step further. The L1 cache. Three pieces of clothing I wear to work everyday; until I wash them over the weekend. A new set is decided every Monday. Simple. Fast. Efficient.
The LRU Algorithm provides one final insight — when you can’t wash the entire pile in one go, start from the bottom and choose as many as you can to wash. They’re the ones to be evicted from the cache first.
This post was inspired by Algorithms to live by. [ Affiliate link ]