IIT Delhi·about 22 hours ago
Understanding CPU branch prediction: Why sorting an array makes code 6x faster
Classic computer architecture experiment: Iterate over 100,000 integers and conditionally sum elements > 128. If the array is unsorted, the CPU's branch predictor misses 50% of the time, stalling the instruction pipeline. If the array is sorted, branch prediction accuracy is ~99%, and execution runs 6x faster. Understanding hardware mechanics makes you a dramatically better programmer.
