A previous post, Timing in an elegant way in C, showed a neat method for profiling using a wrapper function. I am trying to use one of the profiler to profile my class functions.
#include <cmath>
#include <string>
#include <chrono>
#include <algorithm>
#include <iostream>
template<typename Duration = std::chrono::microseconds,
typename F,
typename ... Args>
typename Duration::rep profile(F&& fun, Args&&... args) {
const auto beg = std::chrono::high_resolution_clock::now();
std::forward<F>(fun)(std::forward<Args>(args)...);
const auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<Duration>(end - beg).count();
}
The profiler works for a normal function, but I am struggling to pass a class function to the profiler.
#include "Profiler.h"
int main()
{
Config projConfig = Config(projConfigDir);
std::string imagePath = projConfig.GetTIFF();
ImageVector images_vec = Image::LoadImagesVec(imagePath);
Detector detector = Detector(&projConfig);
auto time = profile<std::chrono::seconds>(&Detector::DetectImages, detector, images_vec);
//detector.DetectImages(images_vec); // if ran without profiler
std::string _detectTime("DetectImages time elapsed: " + std::to_string(time));
Logger::Info(_detectTime.c_str());
}
I am unable to compile the code. I got the following error message.
term does not evaluate to a function taking 2 arguments
Because I cannot pass pointer to a bounded function to the profiler, I tried passing in the function, the object instance to call the function and the function's arguments (not sure if this is the correct way). But I suspect that the profiler is not implemented to handle class methods. If so, how should I modify the profiler so that it can accept class functions?