I am trying to create measuring class with one function that take function to be measured as parameter. Its called ChronoTimer.h content is:
#include <chrono>
template<typename TimeT = std::chrono::milliseconds, class ClockT =
std::chrono::system_clock>
class ChronoTimer {
public:
template<typename F, typename ...Args>
static typename TimeT::rep duration(F func, Args&&... args)
{
auto start = ClockT::now();
func(std::forward<Args>(args)...);
return std::chrono::duration_cast<TimeT>(ClockT::now() - start);
}
};
Then I have "someClass" where is a function i want measure, and a function where I want to measure the function
#include "ChronoTimer.h"
long int someClass::measuredF( long end ) {
long int cnt = 0;
for ( long int i = 0; i < end; ++i ) {
cnt = cnt + i;
}
return cnt;
}
void someClass::someFunction() {
long int end = 10;
auto duration = ChronoTimer<>::duration(someClass::*measuredF, end).count();
}
There is an error and i dont have enough understing/knowledge in C++ to get it working. Appreciate any help, thanks
You should pass a callable object, &someClass::measuredF
is not callable, its requires a someClass
object.
You may do:
void someClass::someFunction() {
long int end = 10;
auto duration = ChronoTimer<>::duration([this](long int end) { this->measuredF(end); }, end).count();
}
or capture all:
void someClass::someFunction() {
long int end = 10;
auto duration = ChronoTimer<>::duration([=]() { this->measuredF(end); }).count();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.