How do I perform an (almost-)branch-less binary search on arbitrary sorted arrays in a preferably portable fashion? (e.g. code that helps compilers generate the CMOV instruction would be great for this.)
By “almost” I mean “containing as few branches as possible”.
Here’s a version of
std::lower_boundwhich had only 1 branch (namely, thebegin != endtest) when I tested it with Visual C++ 2012 (64-bit):32-bit with SSE2 support would probably be able to use the Conditional-Move instruction as well, to gain similar speed.
Now the speed should be competitive with linear search for small arrays… but it might be worth checking.
Interestingly, I found that for a
vector<int>up to size (approximately) 45 on my CPU, a linear search is still faster! Not sure why though, or if my measurement was accurate…Also turns out that this isn’t any faster than a branching binary search on my i5 CPU.