Thats how i do it, not much code, easy to understand, fits my needs:
void bench(std::function<void()> fnBench, std::string name, size_t iterations)
{
if (iterations == 0)
return;
if (fnBench == nullptr)
return;
std::chrono::high_resolution_clock::time_point start, end;
if (iterations == 1)
{
start = std::chrono::high_resolution_clock::now();
fnBench();
end = std::chrono::high_resolution_clock::now();
}
else
{
start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < iterations; ++i)
fnBench();
end = std::chrono::high_resolution_clock::now();
}
printf
(
"bench(*, \"%s\", %u) = %4.6lfs\r\n",
name.c_str(),
iterations,
std::chrono::duration_cast<std::chrono::duration<double>>(end - start).count()
);
}
Usage:
bench
(
[]() -> void // function
{
// Put your code here
},
"the name of this", // name
1000000 // iterations
);