I want to measure the execution time of methods and found the following Topic:
There exists an answer for measuring the function execution time with
#include <iostream>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
template<typename F, typename ...Args>
static typename TimeT::rep execution(F func, Args&&... args)
{
auto start = std::chrono::system_clock::now();
func(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast< TimeT>
(std::chrono::system_clock::now() - start);
return duration.count();
}
};
int main() {
std::cout << measure<>::execution(functor(dummy)) << std::endl;
}
Is it possible to use this code for method measurement? How can i change the template types to pass a method and an object like
measure<>::execution(object, method);
or better
measure<>::execution(object.method(param));
By now, i have the following result:
struct measure
{
template<class T, typename R>
static R execution(T& obj, R(T::*func)())
{
auto start = std::chrono::system_clock::now();
R result = (obj.*func)();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>
(std::chrono::system_clock::now() - start);
return result;
}
};
But i don't know how to pass Args and use it as shown in the example above. I have to execute
measure::execution(obj, method);