Skip to content

Instantly share code, notes, and snippets.

@Gerold103
Last active October 9, 2023 18:52
Show Gist options
  • Save Gerold103/837ba4b7d31eb946bdf0d79d0ea2c3bf to your computer and use it in GitHub Desktop.
Save Gerold103/837ba4b7d31eb946bdf0d79d0ea2c3bf to your computer and use it in GitHub Desktop.
BenchMemoryOrderX86
#include <atomic>
#include <iostream>
#define MEMORY_ORDER std::memory_order_relaxed
// #define MEMORY_ORDER std::memory_order_seq_cst
static constexpr int numberCount = 50;
static constexpr uint64_t targetValue = 1'000'000'000;
static uint64_t getUsec()
{
timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec * 1'000'000 + t.tv_nsec / 1000;
}
int workOrdered(std::atomic_uint64_t& value)
{
uint64_t a = 0;
for (uint64_t i = 0; i < targetValue; ++i)
{
++a;
value.store((uint64_t)&value, MEMORY_ORDER);
}
return a;
}
int main()
{
std::atomic_uint64_t value(0);
uint64_t t1 = getUsec();
workOrdered(value);
uint64_t duration = getUsec() - t1;
std::cout << "Took " << duration << " us" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment