Skip to content

Instantly share code, notes, and snippets.

@alexgpg
Last active February 4, 2022 18:00
Show Gist options
  • Select an option

  • Save alexgpg/1f8d04e4749c49a13e9978a682336bec to your computer and use it in GitHub Desktop.

Select an option

Save alexgpg/1f8d04e4749c49a13e9978a682336bec to your computer and use it in GitHub Desktop.
benchmark::DoNotOptimize() GCC vs clang

Install google benchmark

Ubuntu/debian

sudo apt-get install -y libbenchmark-dev

Arch linix

sudo packan -S benchmark

Run benchmark

git clone https://gist.github.com/1f8d04e4749c49a13e9978a682336bec.git benchmark/
cd benchmark/

Run with GCC

CXX=g++ make 

Run with clang

CXX=clang++ make
#include <benchmark/benchmark.h>
#include <array>
// Easy way to get a big size object.
static std::array<int, 1000000> arr{};
static void BM_Array(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(arr);
}
}
static void BM_PtrToArray(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(&arr);
}
}
BENCHMARK(BM_Array);
BENCHMARK(BM_PtrToArray);
BENCHMARK_MAIN();
.PHONY: all build bench.bin run
all: build run
build: bench.bin
run:
./bench.bin
bench.bin: main.cc
$(CXX) main.cc -g -lbenchmark -lpthread -Ofast -march=native -o bench.bin
asm:
objdump -C -M intel -S ./bench.bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment