High-Level Overview
To understand the true value of Data structures, we have to dig a bit deeper into the way computer works at the fundamentals level.
In order to computer to run the program we wrote, it needs to keep track of things like variables (strings, number, array, boolean...) somewhere in memory.
β These variables are stored in RAM (Random Access Memory)
π Data stored in RAM will be lost when the computer turns off.
β In addition, we also have Storage
π Is the place to store files, images, videos, music files, documents...
π Data on storage is Permanent or Persistent
π When you turn off the laptop, the data still be there when you turn back on.
β CPU (Central Processing Unit)
π think of it as a little worker that does all the calculations, actual work inside our computer.
π€ π€ Storage is great. Why don't we store data in Storage all the time?
In order to run our code, the CPU need access to data stored in the RAM and the Storage.
π But the CPU can access the information in the RAM A LOT FASTER. π€©
Memory & How does the computer store data in the RAM?
π Data stored in Memory of our computer will be stored in form of Byte
π 1 Byte = 8 Bits
π Bit is a tiny electrical switch that can be turned On or Off. But instead of calling on / off, we call it 1
or 0
π Bit the smallest building block of storage.
π 8 bits - 1 byte can make 256 different patterns
π Mathematically: n bits yields 2n patterns (2 to the nth power)
(Image source: ZTM)
Think of RAM in the computer as a massive storage area.
You can call it a bounded canvas.
Each shelf inside the canvas is called a Memory Slot.
We only have a limited amount of Memory Slot.
Each memory slot holds a number - it's a Memory Address.
Our computer uses this number to look for the value stored in the form of 8 bits or 1 byte.
The CPU is connected to the Memory Controller.
Memory Controller does the job of reading, and writing data to the memory.
The direct connection to the Memory Controller is really important because we can access the memory really fast.
The closer the information is to the CPU, the less it has to travel, and then the Faster the program can run. The computer is tuned to run faster when reading memory addresses that are close to each other.
Further optimization, our computer has CPU Cache (LRU Cache)
The CPU has a tiny memory space, where it stores a copy of stuffs that is really recent.
Next time when you want to lookup this piece of information you already visited recently, the process will be much faster.
How computers store a value in RAM
In modern computers, usually we represent an integer in Fixed-width integer (32bits, and 64bits).
For instance, we're using a computer with a 32-bit integer (4 bytes), thereby it's going to take 32 / 8 = 4
memory slots to store an integer inside our RAM, similarly, with 64-bit (8 bytes), it'll take 64 / 8 = 8
memory slots.
π From memory slot number 0 to 3, we stored the value of var a = 1
π From memory slot number 4 to 7, we stored the value of var b = 7