Amortized
Dictionary meaning is spreading out a financial cost, debt, or asset value
into smaller, regular amounts over a set period of time
For data structures it means "average cost per operation over a long
sequence of operations not 1 single operation"
Examples:
1. Inserting into a std::vector or a hash table is Amortized O(1)
Because 99% of the time: The operation is lightning-fast and takes
O(1) constant time because there is open space available
1% of the time: The structure runs out of space. It must allocate a
brand-new, larger chunk of memory and copy all existing elements into it.
This single operation takes O(n) linear time.
Cache Locality
Cache locality refers to the tendency of a CPU to access memory locations
that are physically close to each other and copy those to CPU caches
Cache line is 64 bytes and stores it in the cache.
A Simple Analogy
Imagine you are baking a cake at a kitchen counter (the CPU) and your
ingredients are stored in a pantry in the hallway (the RAM).
• Good Cache Locality: You need sugar. Instead of bringing back just one
teaspoon, you grab the whole bag of sugar and the bag of flour sitting
right next to it, placing them on your counter. When you need flour next,
it’s already right there in front of you.
• Bad Cache Locality: You need sugar, so you walk to the pantry, grab a
teaspoon, and walk back. Next, you need vanilla, which is stored on the
top shelf on the opposite side of the pantry. You have to walk all the way
back to the pantry just for that one item.
1. Vector (Contiguous Memory = Great Locality)
• How it works: Because all elements are side-by-side, when the CPU reads
Element 0, the hardware automatically loads Element 1, Element 2, and
Element 3 into the CPU cache at the exact same time.
• The Result: Iterating through a vector is incredibly fast. The CPU
almost never has to wait for RAM because the next item is already waiting
in the cache.
2. Hash Table (Scattered Memory = Poor Locality)
• In hash table (like one using chained linked lists), the "buckets" might
be an array, but the actual data nodes are scattered all over your RAM at
random addresses.
• The Result: When the CPU looks up an item, it jumps to a bucket, reads a
pointer, and then has to jump to a completely different, random area of
RAM to find the actual data. Every random jump usually results in a cache
miss, forcing the CPU to stop and wait for the slow main RAM to deliver
the data.