Fastest Branchless Binary Search

2023/07/02
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

In this article, the author presents the fastest implementation of a general binary search in C++. The implementation provides the same function interface as std::lower_bound but is twice as fast and shorter. The key to its speed is that it is "branchless," meaning that the if statement compiles down to a conditional move instruction instead of a branch or conditional jump. The article also mentions that there are compiler options, even faster versions, fully branchless implementations, and some caveats that will be explored later on.

The article starts with a brief introduction to binary search, explaining that it is used to find the position where a value would fit in a sorted list. In C++, developers typically use std::lower_bound for this purpose. The author then challenges the readers to write their own binary search implementation as a coding interview question.

For developers who are not familiar with C++, the author assures that understanding the article only requires knowledge of iterators (pointers to elements in an array) and the concept that they can point one past the last array entry. The article concludes with a mention of the desire for a clean and fast bare-metal language to write this code in.

To provide more value to our readers, here's an example of a binary search implementation in C++:

template <typename T>
int binarySearch(const T* arr, int size, const T& value) {
    int left = 0;
    int right = size - 1;

    while (left <= right) {
        int mid = left + (right - left) / 2;

        if (arr[mid] == value) {
            return mid;
        }

        if (arr[mid] < value) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    return -1; // Value not found
}

This implementation takes an array arr, its size size, and the value to search for value. It uses a while loop to iteratively narrow down the search range by comparing the middle element of the current range with the target value. If the middle element is equal to the target value, the function returns the index of that element. If the middle element is less than the target value, the search range is updated to the right half. Otherwise, it is updated to the left half. If the value is not found, the function returns -1.

By keeping up with the latest developments in binary search implementations, developers can optimize their code for faster search operations.