Optimization basics
| Measure first | |
| Memory is the cost | |
| Fast patterns |
Measure first
Time the whole program before optimizing: clock_gettime around the hot loop, or the time command for a first look. Guessing at assembly performance is reliably wrong, so measure, fix one thing, measure again.
Memory is the cost
A register operation is one cycle; a cache hit is a few; a cache miss is hundreds. Most optimization is keeping data in registers and walking memory sequentially, because prefetch makes sequential access nearly free and random access expensive.
Fast patterns
Zero with XOR (xor eax, eax) instead of MOV, use LEA for address math and small multiplies, and unroll small loops when the body is shorter than the loop overhead. But the compiler in -O2 already knows these: beat it only after measuring.
Next: Error codes and logging
Article author: Arthur Isaev