News for hackers. Stuff that matters.
Re:Binary Search Reconsidered (2018) by ncmncm on Saturday December 26, 2020 @01:06AM (#25541769)
What nobody says about binary search is that it is very, very slow on modern hardware.

The reasons it is slow are interesting and useful to understand.

On modern equipment, to get decent performance, everything needs to pipeline, so you start an operation and get the answer out 3, 5, sometimes 12 cycles later. In the meantime, you're pushing new operations into the pipeline so that useful answers will come out the cycle after. To keep pipelines (plural; there are lots) full, we have "branch prediction": the CPU doesn't know which way an upcoming conditional branch will go, so it guesses. It has a neural network to remember how this branch went other times, that is quite clever. When it guesses right, all is good, and the pipeline stays full. When it guesses wrong, it has gone off doing lots of wrong computations that have to be thrown away, and the pipeline restarted at the correct spot.

Binary search presents the worst case for prediction. At each stage, any prediction is 50% likely to be wrong. You hardly get two steps into your pipeline before it turns out you were wrong, and have been doing the wrong thing.

There are ways to reduce the worst effects, but they make the algorithm much less pleasant to read, and even trickier to get right. One is to use clever arithmetic to arrange that the same instruction sequence runs whichever way the choice goes. Another is to split the range into more than two subranges, so the choice is made less often--log base 3 or -base 4 iterations, instead of two. Maybe you pre-fetch values you expect to look at soon: half of them will be ones you won't actually need, and you won't know which until later, but you didn't need to wait for the right one.

Once a range gets small enough, you do better searching sequentially, because CPUs are tuned to do that very fast.

Hash tables avoid all this foolishness, and (so) can be very fast.

To write good, fast code for things like binary search, intuition is entirely inadequate. You really need to pay a lot of attention to "performance counter" registers modern chips have, that can tell you about prediction failures and cache misses that slow you to a crawl. It is humbling to discover how wrong all we learned in school, about algorithm performance, is.

When you home in on a code sequence that minimizes pipeline bubbles, you have to expect that the next generation of CPU chips will have completely different details, and that what was fast is now slow.

The deep lesson is that we never know if our code is fast until we find code that is faster; what seemed fast becomes slow in an instant. All the other old faithful algorithms, like Quicksort, can be easily 2x faster or slower according to obscure details that are hard to reason about.

It makes some people decide that 2x, 4x, 100x, or 1000 times slower than necessary ought to be considered fast enough.

Re:Binary Search Reconsidered (2018) by Dowwie on Saturday December 26, 2020 @06:19PM (#25547265)
I really enjoyed reading your comment. Do you maintain a blog where you discuss such matters?
Re:Binary Search Reconsidered (2018) by 6nf on Saturday December 26, 2020 @03:31AM (#25542165)
You’re not wrong, just off topic.