How deleting one if-statement made this filter 4x faster
3 months ago
A developer deleted one if-statement from ordinary code. It ran up to four times faster. Here's why that makes sense.
Ask about this presentation
Chapters
0:00How deleting one if-statement made this filter 4x faster
0:07Slowest when it keeps half. Faster when it keeps almost all
0:21Sort the same numbers first, and it runs 4.5x faster
0:34Your processor guesses which way each if goes, and races ahead
1:02Delete the if. Always write, then move the cursor by 1 or 0
1:21It's a trade: keep almost nothing, and the plain version wins
1:32The slow part was never the work. It was the guessing
Show transcriptHide transcript
A developer deleted one if-statement from ordinary code. It ran up to four times faster. Here's why that makes sense.
The code is a filter: run through a million numbers, keep the ones past a cutoff. Yet it is slowest when it keeps half of them, and more than twice as fast when it keeps almost all. The input never changes.
The obvious fix barely helped, about two percent. So copying was never the cost. The real clue: sort the same numbers first, and the slow case runs four and a half times faster. Only the order changed.
A modern processor does not finish one step before starting the next. At every if, the processor cannot wait to see which way to go. So it guesses, and races ahead. On sorted data the answer barely changes, so the guess is nearly always right. On shuffled data, at the halfway mark, the guess is a coin flip. The processor is wrong about half the time, and every wrong guess throws away the work it already started. That wasted work, not the copying, is the slow part.
So the developer deleted the decision. Always write the number down. Then move a cursor forward by the comparison: one when the number passes, zero when it does not. Cut the leftover tail. Nothing is left to guess, so the timing goes flat. Worst case, about four milliseconds down to one.
It is a trade. Keep one percent of the numbers, and the plain version wins. Deleting the decision gives up the best case to protect the worst. So save it for a loop you measured and found slow.
The lesson reaches past this filter. Sometimes the slow part of your code is not the work. It is your processor guessing wrong about what comes next.
Up next
1:48Writing a note down as one thing is what made it fast
@the-daily-build
3 months ago
1:51Nothing on the rig measures speed
@the-daily-build
3 months ago
1:53A file gets smaller when you can guess what is in it
@the-daily-build
3 months ago
1:55The seal on an AI's private thinking had one key for the whole company
@the-daily-build
3 months ago
1:50One row for every sellable unit, and each checkout takes any free one
@the-daily-build
3 months ago
1:42A single CPU instruction, stretched from a blink to a full minute
@the-daily-build
3 months ago
1:44You can't be best at everything
@the-daily-build
3 months ago
1:42Software gives you five skin tones, or sixteen million
@the-daily-build
3 months ago
1:43A Self-Spreading Worm Got Into the Code Behind Billions of Installs
@the-daily-build
3 months ago
1:34The AI session you can’t take with you
@the-daily-build
3 months ago
1:42He Walked Through His House Before It Was Built, at Full Size, in a Headset
@the-daily-build
4 months ago
1:38500 Lines of Code That Draw 3D Without a Graphics Card
@the-daily-build
4 months ago