Skip to content

Instantly share code, notes, and snippets.

@lambday
Created April 1, 2016 15:47
Show Gist options
  • Select an option

  • Save lambday/ccc7f289e965223e48f5fbda5882313b to your computer and use it in GitHub Desktop.

Select an option

Save lambday/ccc7f289e965223e48f5fbda5882313b to your computer and use it in GitHub Desktop.
#include <DynamicInterface.hpp>
#include <CRTPInterface.hpp>
#include <benchmark/benchmark.h>
#include <memory>
unsigned long N = 10000;
auto dynobj = std::make_unique<DynamicImplementation>();
auto crtpobj = std::make_unique<CRTPImplementation>();
void run_dynamic(DynamicInterface* obj) {
for (unsigned long i = 0; i < N; ++i) {
for (unsigned long j = 0; j < i; ++j) {
obj->tick(j);
}
}
}
template <typename Implementation>
void run_crtp(CRTPInterface<Implementation>* obj) {
for (unsigned long i = 0; i < N; ++i) {
for (unsigned long j = 0; j < i; ++j) {
obj->tick(j);
}
}
}
static void BM_DynamicInterface(benchmark::State& state)
{
while (state.KeepRunning())
{
run_dynamic(dynobj.get());
}
}
BENCHMARK(BM_DynamicInterface);
static void BM_CRTPInterface(benchmark::State& state)
{
while (state.KeepRunning())
{
run_crtp(crtpobj.get());
}
}
BENCHMARK(BM_CRTPInterface);
BENCHMARK_MAIN();
[lambday@lambday.iitb.ac.in benchmark]$ g++ -O3 -std=c++14 benchmark_google.cpp -lbenchmark -lpthread -I.
[lambday@lambday.iitb.ac.in benchmark]$ ./a.out
Run on (4 X 1235.3 MHz CPU s)
2016-04-01 21:17:06
***WARNING*** CPU scaling is enabled, the benchmark real time measurements may be noisy and will incur extra overhead.
Benchmark Time(ns) CPU(ns) Iterations
----------------------------------------------------
BM_DynamicInterface 106829282 106696857 7
BM_CRTPInterface 12057762 12054103 58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment