30/32 · 5 min
Why the disk is the slowest thing you own
Every performance problem in this subject comes back to one table.
| Operation | Time | If a processor cycle were one second |
|---|---|---|
| Processor cycle | 0.3 ns | 1 second |
| L1 cache | 1 ns | 3 seconds |
| Main memory | 100 ns | 5 minutes |
| SSD read | 150 us | 6 days |
| Disk seek | 10 ms | 1 year |
| Network round trip | 150 ms | 15 years |
A disk seek is a billion times slower than an instruction. Nothing you do to your code matters if it causes one you could have avoided.
What the operating system does about it
Buffering. Read a whole block even when asked for a byte, because the next byte is almost certainly wanted and the seek is already paid for.
Caching. Keep recently used blocks in RAM. This is why the second read of a file is instant, and why a machine with plenty of free memory feels faster: the memory is not idle, it is full of disk.
Reordering. Given several pending reads, serve them in an order that minimises head movement rather than in the order they arrived.
Scheduling the disk arm
For requests at cylinders 98, 183, 37, 122, 14, 124, 65, 67 with the head at 53:
- FCFS serves them in order and travels 640 cylinders.
- SSTF always takes the nearest next, travels 236, and can starve a distant request forever.
- SCAN sweeps to one end and back, like a lift. About 236 as well, and no starvation.