WHICH WINS?

Which Wins?

Kage races real code on a real laptop.

Two rounds each. Every number comes from a real run, and you can run every race yourself.

KAGE CHECKED THE NUMBERS ✓
Kage, the ink ninja, pointing

Every race so far

Times come from one Intel Core i9-14900HX laptop. Yours will differ; the winner of each round should not.

Which Wins? No. 6: CPU vs GPU
WHICH WINS? #6

CPU vs GPU

ROUND 1Multiply two 4096 x 4096 matricesGPU 14 ms, CPU 162 ms: GPU 11x fasterWinner: GPU
ROUND 210,000 steps, each needing the step before (x = x * 0.999 + 1)CPU 36 ms, GPU 156 ms: CPU 4x fasterWinner: CPU

Work that splits into independent pieces goes to the GPU; work that runs step by step stays on the CPU.

Interview question

Why do AI models run on GPUs?
They are mostly huge matrix multiplies, where every output cell can be computed at the same time.

Which Wins? No. 8: Python vs C
WHICH WINS? #8

Python vs C

ROUND 1Add up the squares of 100,000,000 numbers in a plain loopC 63 ms, Python 3,882 ms: C 62x fasterWinner: C
ROUND 2Same job, Python makes one NumPy call: np.dot(a, a)Python 34 ms, C's loop 63 msWinner: Python (NumPy)

A plain Python loop is slow; one library call that runs the loop in compiled code is fast.

Interview question

Why is Python slow, and why is it still used for AI?
The interpreter runs a loop one step at a time; the heavy loops in AI run in compiled code underneath (NumPy, PyTorch).

Which Wins? No. 9: RAM vs SSD
WHICH WINS? #9

RAM vs SSD

ROUND 1Look up 100,000 records of 4 KB at random places in the same 1 GBRAM 46 ms, SSD 11,249 ms: RAM 245x fasterWinner: RAM
ROUND 2The program holding the data is killed (kill -9)RAM: 0 bytes left. SSD file: all 1,073,741,824 bytes back, same checksumWinner: SSD

RAM is fast but forgets; disk is slow but remembers. Real systems use both.

Interview question

Why do databases write a log to disk before changing data?
So that after a crash they can rebuild every change from the log; memory is wiped when the process dies.

Which Wins? No. 10: Threads vs Processes
WHICH WINS? #10

Threads vs Processes

ROUND 18 jobs of pure maths, all started at once8 processes 132 ms, 8 threads 702 ms: processes 5.3x fasterWinner: Processes
ROUND 21,000 tiny jobs, a new worker started for eachThreads 65 ms, processes 1,124 ms: threads 17x fasterWinner: Threads

Heavy calculation goes to processes; many small or waiting jobs go to threads.

Interview question

What is the GIL?
In standard Python only one thread runs Python code at a time. Threads still help while waiting on files or the network, and they are cheap to start.

Which Wins? No. 11: Index vs No Index
WHICH WINS? #11

Index vs No Index

ROUND 1Find one user by email among 5,000,000With index 0.0031 ms, without 121 ms: about 39,000x fasterWinner: Index
ROUND 2Add 1,000,000 new usersWithout index 507 ms, with index 920 ms: 1.8x faster; the index also doubled the file (191 MB to 381 MB)Winner: No index

Lots of reads: add the index. Lots of writes: keep indexes few.

Interview question

Why not index every column?
Each index makes every insert and update slower and takes disk space.

Which Wins? No. 12: Array vs Linked List
WHICH WINS? #12

Array vs Linked List

ROUND 1Add up 10,000,000 valuesArray 5.3 ms, shuffled linked list 1,060 msWinner: Array
ROUND 2Insert 2,000 values at the front of 200,000Linked list 0.002 ms, array 42 msWinner: Linked list

Arrays win at reading in order (the CPU fetches neighbours together); linked lists win at inserting at the front.

Interview question

Why is a linked list slow to walk even though each step is O(1)?
Its nodes are scattered in memory, so almost every step is a cache miss; an array's values sit next to each other.

The cheat sheet

Every race on one page: both rounds, the winner, the rule to remember and the interview question it answers.

Kage cheering