Last active
February 6, 2021 08:44
-
-
Save osadalakmal/e0a3bd5c8f9d27c2de20163fde1c4af9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <memory> | |
#include <thread> | |
#include <benchmark/benchmark.h> | |
void f1(bool shouldMallocInLoop) | |
{ | |
auto n = 0; | |
void *c = nullptr; | |
for (int i = 0; i < 10; ++i) { | |
if (shouldMallocInLoop) | |
c = malloc(10); | |
++n; | |
auto d = (void*)(c); | |
if (shouldMallocInLoop) | |
free(c); | |
} | |
} | |
static void BM_TwoThreadsRunningNoMalloc(benchmark::State& state) | |
{ | |
for (auto _ : state) { | |
std::thread t1(f1, false); | |
std::thread t2(f1, false); | |
t1.join(); | |
t2.join(); | |
} | |
} | |
static void BM_TwoThreadsRunningWithMalloc(benchmark::State& state) | |
{ | |
for (auto _ : state) { | |
std::thread t1(f1, true); | |
std::thread t2(f1, true); | |
t1.join(); | |
t2.join(); | |
} | |
} | |
static void BM_SerialRunningWithMalloc(benchmark::State& state) | |
{ | |
for (auto _ : state) { | |
f1(true); | |
f1(true); | |
} | |
} | |
static void BM_SerialRunningNoMalloc(benchmark::State& state) | |
{ | |
for (auto _ : state) { | |
f1(false); | |
f1(false); | |
} | |
} | |
BENCHMARK(BM_TwoThreadsRunningNoMalloc); | |
BENCHMARK(BM_TwoThreadsRunningWithMalloc); | |
BENCHMARK(BM_SerialRunningNoMalloc); | |
BENCHMARK(BM_SerialRunningWithMalloc); | |
BENCHMARK_MAIN(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment