Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save lambday/c3a3d3cae854d5c1d1c9be25b1744358 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>();
DynamicImplementation dynobj;
CRTPImplementation crtpobj;
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);
}
}
BENCHMARK(BM_DynamicInterface);
static void BM_CRTPInterface(benchmark::State& state)
{
while (state.KeepRunning())
{
run_crtp(&crtpobj);
}
}
BENCHMARK(BM_CRTPInterface);
BENCHMARK_MAIN();
[[email protected] benchmark]$ g++ -O3 -std=c++14 benchmark_google.cpp -lbenchmark -lpthread -I.
[[email protected] benchmark]$ ./a.out
Run on (4 X 1217.33 MHz CPU s)
2016-04-01 21:20:36
***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 12017047 12016833 42
BM_CRTPInterface 12024512 12024316 57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment