Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

*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 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.


Wouldn't cache line misses dominate? Linear search benefits from prefetch.


Modern architectures really necessitate a good understanding of the instruction pipeline and caches to squeeze out the best performance.

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).


True, if you're out of cache, there's a goldilocks zone where linear search works better. I mostly disregard that because *chotomic search is equivalent/quicker in cache and for small arrays, and quicker for large arrays.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: