Timing in an elegant way in c++ -


i interested in timing execution time of free function or member function (template or not). call thefunc function in question, call being

thefunc(/*parameters*/); 

or

returntype ret = thefunc(/*parameters*/); 

of course wrap these function calls follows :

double duration = 0.0 ; std::clock_t start = std::clock(); thefunc(/*parameters*/); duration = static_cast<double>(std::clock() - start) / static_cast<double>(clocks_per_sec); 

or

double duration = 0.0 ; std::clock_t start = std::clock(); returntype ret = thefunc(/*parameters*/); duration = static_cast<double>(std::clock() - start) / static_cast<double>(clocks_per_sec); 

but more elegant this, namely (and on stick void return type) follows :

timer thetimer ; double duration = 0.0; thetimer(*thefunc)(/*parameters*/, duration); 

where timer timing class design , allow me write previous code, in such way after exectution of last line of previous code double duration contain execution time of

thefunc(/*parameters*/); 

but don't see how this, nor if syntax/solution aim optimal...

with variadic template, may do:

template <typename f, typename ... ts> double time_function(f&& f, ts&&...args) {     std::clock_t start = std::clock();     std::forward<f>(f)(std::forward<ts>(args)...);     return static_cast<double>(std::clock() - start) / static_cast<double>(clocks_per_sec); } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -