If the data to be binary searched is static, that is, if it doesn't change, and if it fits entirely in RAM, then what I would do is as follows:<p>1) Pre-compute the first mid/center element, C1.<p>2) Move the data for this mid/center element to the first item in a new array.<p>3) Pre-compute the next two mid/center elements, that is, the ones between the start of the data and C1 (C2), and the one between C1 and the end of data (C3).<p>4) Move the data from C2 and C3 positions to the next positions in our array, the 2nd and 3rd position.<p>5) Keep repeating this pattern. For every iteration/split there are 2 times the amount of midpoints/data than the previous iteration. Order these linearly as the next items in our new array.<p>When you're done, two things will occur.<p>1) You will use a slightly different binary search algorithm.<p>That is because you <i>no longer need to compute a mid-point at every iteration</i>, those are now <i>pre-computed</i> in the ordered array.<p>2) Because the data is now ordered, it becomes possible to
load the tip of that data into the CPU's L1, L2, and L3 cache. If let's say your binary search takes 16 iterations to complete, then you might get a good headstart of 5-8 iterations (or more, depending on cache size and data size) of that data being in cache RAM, which will make those iterations MUCH faster.<p>Also, (and this is just me), but if your program has appropriate permissions to temporarily shut off interrupts (x86 cli sti -- or OS API equivalent), then this search can be that much faster (well, depends on what the overhead for cli/sti and API calls are, but test, test, test! (also, always shut off the network and other threads when you're testing, as they can skewer the results!) <g>)<p><a href="https://en.wikipedia.org/wiki/Memory_hierarchy" rel="nofollow">https://en.wikipedia.org/wiki/Memory_hierarchy</a><p>"Almost all programming can be viewed as an exercise in caching." --Terje Mathisen<p>"Assume Nothing" --Mike Abrash<p>Also, there is no such thing as the fastest binary search algorithm... there's always a better way to do them...<p>To paraphrase Bruce Lee:<p><i>"There are no mountain tops, there is only an endless series of plateaus, and you must ever seek to go beyond them..."</i>