This is not a valid drop in replacement for lower_bound. Accessing iterators as front[index] is only for random access iterators like vector has. The author may have realized this if they benchmarked on other containers.A forward iterator must be advanced and then dereferenced.
Can you do binary search without random access? I would not expect so. Am I missing a point that someone could explain?
Fastest branchless binary search
51–60 of 155 posts
Re: Fastest branchless binary search
#52Is that still lower_bound? Maybe I am misreading the code but it looks like this returns any match, not the earliest match (when there are dupes). It’s common to have multiple matches even in a unique list if the comparison function is say looking for a certain string prefix to do autocomplete, but we want the earliest in the list.
I guess it’s good to have the option of not caring if you want even more speed
template
constexpr ForwardIt super_optimized_lower_bound(
ForwardIt first, ForwardIt last, const T& value, Compare comp) {
return first;
}
It doesn't really work like for some cases std::lower_bound but it is super fastRe: Fastest branchless binary search
#53This is not a valid drop in replacement for lower_bound. Accessing iterators as front[index] is only for random access iterators like vector has. The author may have realized this if they benchmarked on other containers.A forward iterator must be advanced and then dereferenced.
Re: Fastest branchless binary search
#54Re: Fastest branchless binary search
#55Re: Fastest branchless binary search
#56I wish all blog posts started the way his does: "You’re a busy person so I’ll first jump right to it. Here it is, the fastest general (and simple) binary search C++ implementation:"
Re: Fastest branchless binary search
#57> If only there was a clean fast bare-metal language to write all this in.. The author includes a footnotes for "BUT RUST.." and "BUT ZIG..", but how about Nim? Looks like there is a native library implementation of `lowerBound` https://github.com/nim-lang/Nim/blob/version-2-0/lib/pure/al... Yes, it's not a "bare-metal" language, but it compiles to one (or two), so it would be interesting to see what it compiles to h…
Re: Fastest branchless binary search
#58Having said that, I did enjoy the post.
Re: Fastest branchless binary search
#59> If only there was a clean fast bare-metal language to write all this in.. The author includes a footnotes for "BUT RUST.." and "BUT ZIG..", but how about Nim? Looks like there is a native library implementation of `lowerBound` https://github.com/nim-lang/Nim/blob/version-2-0/lib/pure/al... Yes, it's not a "bare-metal" language, but it compiles to one (or two), so it would be interesting to see what it compiles to h…
> Also, what's wrong with C? Oodles and oodles of undefined behaviour, for example. C ain't clean.
Re: Fastest branchless binary search
#60Every post like this makes me update my junior engineer watchlist. Then I have to go patiently explain why mucking about in the code to save one instruction because you read a blog post is a horrible idea. Easily half of all silly junior code is pointless optimization. Having said that, I did enjoy the post.
The junior employees I have had to deal with tended to build towering abstractions that no one really needed, rather than optimising hot loops.