CppBenchmark  1.0.4.0
C++ Benchmark Library
threads.cpp

Threads integer increment benchmark

//
// Created by Ivan Shynkarenka on 23.07.2015
//
#include <atomic>
#include <mutex>
const auto settings = CppBenchmark::Settings().ThreadsRange(1, 8, [](int from, int to, int& result) { int r = result; result *= 2; return r; });
class UnsynchronizedFixture
{
protected:
int counter;
};
class AtomicFixture
{
protected:
std::atomic<int> counter;
};
class MutexFixture
{
protected:
std::mutex mutex;
int counter;
};
BENCHMARK_THREADS_FIXTURE(UnsynchronizedFixture, "unsynchronized++", settings)
{
++counter;
}
BENCHMARK_THREADS_FIXTURE(AtomicFixture, "std::atomic++", settings)
{
++counter;
}
BENCHMARK_THREADS_FIXTURE(MutexFixture, "std::mutex++", settings)
{
std::scoped_lock lock(mutex);
++counter;
}
Benchmark settings.
Definition: settings.h:32
Settings & ThreadsRange(int from, int to)
Add new threads range to the benchmark running plan.
Definition: settings.cpp:67
CppBenchmark definitions.
#define BENCHMARK_MAIN()
Benchmark main entry point macro.
Definition: cppbenchmark.h:46
#define BENCHMARK_THREADS_FIXTURE(fixture,...)
Benchmark threads with fixture register macro.
Definition: cppbenchmark.h:224