Skip to content

Instantly share code, notes, and snippets.

@RecursiveG
Created February 9, 2024 07:28
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 RecursiveG/5361f6da8814f3f11bb63581f401cd87 to your computer and use it in GitHub Desktop.
Save RecursiveG/5361f6da8814f3f11bb63581f401cd87 to your computer and use it in GitHub Desktop.
#include <benchmark/benchmark.h>
// ----------------------------------------------------------
// Benchmark Time CPU Iterations
// ----------------------------------------------------------
// BM_StaticFalse 0.228 ns 0.227 ns 1000000000
// BM_StaticTrue 0.457 ns 0.456 ns 1000000000
// BM_DynamicFalse 0.228 ns 0.228 ns 1000000000
// BM_DynamicTrue 0.458 ns 0.457 ns 1000000000
void func_static_false() {
benchmark::DoNotOptimize(42);
}
void func_static_true() {
benchmark::DoNotOptimize(42);
benchmark::DoNotOptimize(42);
}
void func_dynamic(bool val) {
if (val) {
benchmark::DoNotOptimize(42);
}
benchmark::DoNotOptimize(42);
}
static void BM_StaticFalse(benchmark::State& state) {
for (auto _ : state) {
func_static_false();
}
}
BENCHMARK(BM_StaticFalse);
static void BM_StaticTrue(benchmark::State& state) {
for (auto _ : state) {
func_static_true();
}
}
BENCHMARK(BM_StaticTrue);
static void BM_DynamicFalse(benchmark::State& state) {
for (auto _ : state) {
func_dynamic(false);
}
}
BENCHMARK(BM_DynamicFalse);
static void BM_DynamicTrue(benchmark::State& state) {
for (auto _ : state) {
func_dynamic(true);
}
}
BENCHMARK(BM_DynamicTrue);
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment