Linear vs. Binary Search
schani.wordpress.com
Linear vs. Binary Search
1–10 of 19 posts
Re: Linear vs. Binary Search
#2Binary Search eliminates Branch Mispredictions http://www.pvk.ca/Blog/2012/07/03/binary-search-star-elimina...
Re: Linear vs. Binary Search
#3Of particular interest: Binary Search eliminates Branch Mispredictions http://www.pvk.ca/Blog/2012/07/03/binary-search-star-elimina...
Re: Linear vs. Binary Search
#4Of particular interest: Binary Search eliminates Branch Mispredictions http://www.pvk.ca/Blog/2012/07/03/binary-search-star-elimina...
*When it compiles to CMOV intructions.
The second one is that, when linear search outperforms binary search, it does so by breaking out of the search, which needs conditional branches. Binary search, despite its bad reputation, has conditional branches that are easily converted to conditional moves or masks; even a loopy implementation is amenable to trip count prediction (it's a function of the log of the size of the array). If we must avoid mispredicted branches, binary search is intrinsically a better option than linear search.
Re: Linear vs. Binary Search
#5Earlier quoted context omitted.
*When it compiles to CMOV intructions.
I had two points in that post. The first, obvious, one is that binary search can be micro-optimised to combine decent algorithmic properties with an enviable constant factor. The second one is that, when linear search outperforms binary search, it does so by breaking out of the search, which needs conditional branches. Binary search, despite its bad reputation, has conditional branches that are easily converted to co…
Re: Linear vs. Binary Search
#6Earlier quoted context omitted.
I had two points in that post. The first, obvious, one is that binary search can be micro-optimised to combine decent algorithmic properties with an enviable constant factor. The second one is that, when linear search outperforms binary search, it does so by breaking out of the search, which needs conditional branches. Binary search, despite its bad reputation, has conditional branches that are easily converted to co…
Wouldn't cache line misses dominate? Linear search benefits from prefetch.
If I remember correctly, Python's hashtables are initialized with 8 buckets that are linearly searched and then switched to a real hashtable implementation when grown past that size.
I have worked with the L4 microkernel where sooo much emphasis was put on keeping instruction and data footprints as small as possible every time the kernel is entered in order not to dirty i- and d-caches.
And I have also seen game engine developers do amazing things in this regard. An interesting development in the gaming space is data-oriented-design that deviates from OOP among other things for performance and parallelization. See http://www.slideshare.net/mobile/cellperformance/data-orient... (though I don't agree with the three "lies" mentionened, I do like the data-centric approach).
Re: Linear vs. Binary Search
#7Earlier quoted context omitted.
I had two points in that post. The first, obvious, one is that binary search can be micro-optimised to combine decent algorithmic properties with an enviable constant factor. The second one is that, when linear search outperforms binary search, it does so by breaking out of the search, which needs conditional branches. Binary search, despite its bad reputation, has conditional branches that are easily converted to co…
Wouldn't cache line misses dominate? Linear search benefits from prefetch.
Re: Linear vs. Binary Search
#8This will work for n > 1), which is one subtraction per iteration more.
Making middle an unsigned int should make (min + max) >> 1 work for all int values.
Re: Linear vs. Binary Search
#9int middle = (min + max) >> 1; This will work for n > 1), which is one subtraction per iteration more. Making middle an unsigned int should make (min + max) >> 1 work for all int values.
If min+max is a negative signed int, then the result of right shifting is implementation dependent.
Instead, you could do
int middle = (unsigned)(min + max) >> 1;
Still, this relies on the original indices falling in the non-negative range of signed ints.Re: Linear vs. Binary Search
#10Of particular interest: Binary Search eliminates Branch Mispredictions http://www.pvk.ca/Blog/2012/07/03/binary-search-star-elimina...
> If you’re very, very serious about performance and know the array size statically, completely unroll the binary search.
and pvk says:
> I’ll focus on one specific case that is of interest to me: searching a short or medium-length sorted vector of known size.
"Known" here means "known at compile time". And of course he concludes:
> In general, I’d just stick to binary search: it’s easy to get good and consistent performance from a simple, portable implementation.
Both bloggers use conditional moves to avoid conditional branches; use unrolled versions; and of course use benchmarks to arrive at their conclusions.