Skip to content

Instantly share code, notes, and snippets.

@heatd

heatd/mb.cpp Secret

Created November 25, 2024 00:26
Show Gist options
  • Save heatd/f0f24a2b207118b193ff682d3ee4d901 to your computer and use it in GitHub Desktop.
Save heatd/f0f24a2b207118b193ff682d3ee4d901 to your computer and use it in GitHub Desktop.
#include <benchmark/benchmark.h>
static void mfence_bench(benchmark::State& state)
{
for (auto _ : state) {
__asm__ __volatile__("mfence" ::: "memory");
}
}
static void add_stack(benchmark::State& state)
{
for (auto _ : state) {
__asm__ __volatile__("lock addq $0, (%%rsp)" ::: "memory");
}
}
static unsigned long shite_global;
static void add_global(benchmark::State& state)
{
for (auto _ : state) {
__asm__ __volatile__("lock addq $0, %0" :: "m"(shite_global) : "memory");
}
}
BENCHMARK(mfence_bench);
BENCHMARK(add_stack);
BENCHMARK(add_global);
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment