Article View: comp.lang.c++
Article #734Faster lower_bound / upper_bound
From: Bonita Montero
Date: Mon, 02 Nov 2026 09:24
Date: Mon, 02 Nov 2026 09:24
47 lines
2633 bytes
2633 bytes
I wondered why lower_bound and upper_bound are almost twice as fast
with clang-cl than with MSVC. I inspected the code and MSVC didn't
use conditional moves (see below) and the non-predictible branches
did really hurt.
So I wrote two generic functions which do the same and make it easier
for MSVC to do the optimization. And it worked: The MSVC-code is only
slightly slower than the clang-cl-code with that.
#pragma once
#include <type_traits>
#include <functional>
template<typename Iterator, typename Ref, typename Cmp =
std::less<std::iter_value_t<Iterator>>>
inline Iterator xlower_bound( Iterator begin, Iterator end, const Ref
&value, Cmp cmp = Cmp() )
{
for( size_t n = end - begin; n; )
{
size_t
mid = n / 2,
nRight = n - mid - 1;
Iterator right = begin + mid + 1;
bool less = cmp( begin[mid], value );
begin = less ? right : begin;
n = less ? nRight : mid;
}
return begin;
}
template<typename Iterator, typename Ref, typename Cmp =
std::less<std::iter_value_t<Iterator>>>
inline Iterator xupper_bound( Iterator begin, Iterator end, const Ref
&value, Cmp cmp = Cmp() )
{
for( size_t n = end - begin; n; )
{
size_t
mid = n / 2,
nRight = n - mid - 1;
Iterator right = begin + mid + 1;
bool less = cmp( value, begin[mid] );
begin = less ? begin : right;
n = less ? mid : nRight;
}
return begin;
}
Technical Details
Message-ID:
<11c9ks1$2nfti$1@raubtier-asyl.eternal-september.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mailShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail From: Bonita Montero <Bonita.Montero@gmail.com> Newsgroups: comp.lang.c++ Subject: Faster lower_bound / upper_bound Date: Mon, 2 Nov 2026 10:24:16 +0100 Organization: A noiseless patient Spider Lines: 47 Message-ID: <11c9ks1$2nfti$1@raubtier-asyl.eternal-september.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 02 Nov 2026 09:24:17 +0000 (UTC) Injection-Info: raubtier-asyl.eternal-september.org; logging-data="2867122"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/JgQz0u6pTVl4afdm8uBbzka9Zm7eVTZQ="; posting-host="dbffc34a04661a78eeca19bb42575b43" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:jk8CrYxcBJBl6xt9T4Yr1iB2LlU= sha256:QmIHjB3aER6OITRVu/weWPrprCD41iFzsfkyRnCrLuo= sha1:t4OQF1NeyLEri2ef9pUb8JJEnzY= sha256:X+97rkeEzfnSE2Q1I7d9woBlX6vdp5irzB1bd0Zekas= Content-Language: de-DE