Created
July 22, 2023 22:25
-
-
Save ttsugriy/7f2a4da2902d5ea8476c2e9077933e49 to your computer and use it in GitHub Desktop.
midpoint benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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