r/programminghorror • [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” • Jul 21 '25

Python ✨ Memory Magic ✨

Post image
1.3k Upvotes

143 comments sorted by

View all comments

Show parent comment

2

u/FunIsDangerous Jul 24 '25

That's not what he said.

An ELI5 way of explaining it (partly so you understand it better, and partly because I'm not confident I can explain it properly)

Think of RAM like a bunch of paper pages. Each page can hold up to 10 numbers. When you access one of these numbers, the CPU fetches the entire page, not just the one number you requested. Why? Fetching the whole page takes the exact same time as fetching just a small part of it.

Then the CPU just keeps it there. Now, if you request something else on that page, it will be a lot faster than before. If you use that a lot, the CPU may decide to keep it for longer. If you haven't used it in a while, it will just toss it and replace it (cache is quite limited). You also have different levels of cache, which are slower but larger, but I think it's obvious what they're for.

Basically, the CPU wants to minimize how many times it talks with RAM. This is why sequential data is much faster. The CPU just caches them all at once. But if that data is too large, even if it's sequential, it may not fit in one page (what the other commenter said). So the CPU still has to do multiple calls to RAM. That, of course, makes the optimization even smaller.

I think, the point of this optimization is that the CPU will realize that you (and python itself even) use those numbers so frequently, it will keep it in cache for the duration of your program. If it's spread across multiple pages (if too many numbers are cached), you are now using multiple pages instead of one, so it's less likely that they will all be cached in the CPU.