Consider the following code to measure the time spent by a function:
// Preprocessor
#include <iostream>
#include <chrono>
// Benchmarking function
template <class Function, class... Args>
double benchmark(Function&& f, Args&&... args)
{
using clock = std::chrono::high_resolution_clock;
using duration = std::chrono::duration<double>;
auto tbegin = clock::now();
std::forward<Function>(f)(std::forward<Args>(args)...);
auto tend = clock::now();
return std::chrono::duration_cast<duration>(tend-tbegin).count();
}
// Some example
unsigned int f(unsigned int n)
{
unsigned int r = 0;
for (unsigned int i = 0; i < n; ++i)
for (unsigned int j = 0; j < n; ++j)
r += (i*n)%(j+i+1);
return r;
}
// Main function
int main()
{
std::cout<<f(8192)<<std::endl; // Takes time
std::cout<<benchmark(f, 8192)<<std::endl; // Takes no time
return 0;
}
A problem arises when the function f
has no side effect: the compiler does not execute it. Is there a way to modify my benchmark
function in a generic way in order to force the execution of f
and prevent optimization.