Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created July 22, 2023 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttsugriy/7f2a4da2902d5ea8476c2e9077933e49 to your computer and use it in GitHub Desktop.
Save ttsugriy/7f2a4da2902d5ea8476c2e9077933e49 to your computer and use it in GitHub Desktop.
midpoint benchmark
#include <numeric>
int midpointb(int l, int r) {
return (l + r) >> 1;
}
int midpointw(int l, int r) {
return l + (r - l) / 2;
}
int midpoint(int l, int r) {
return static_cast<unsigned int>(l + r) >> 1;
}
int L = std::numeric_limits<int>::max();
int R = L - 2;
static void MidpointW(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(midpointw(L, R));
}
}
BENCHMARK(MidpointW);
static void Midpoint(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(midpoint(L, R));
}
}
BENCHMARK(Midpoint);
static void StdMidpoint(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(std::midpoint(L, R));
}
}
BENCHMARK(StdMidpoint);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment