I had time measuring pretty much figured out when I ended up using this structure.
#include <iostream>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure
{
template<typename F>
static typename TimeT::rep execution(F const &func)
{
auto start = std::chrono::system_clock::now();
func();
auto duration = std::chrono::duration_cast< TimeT>(
std::chrono::system_clock::now() - start);
return duration.count();
}
};
a use case would be
struct functor
{
int state;
functor(int state) : state(state) {}
void operator()() const
{
std::cout << "In functor run for ";
}
};
void func()
{
std::cout << "In function, run for " << std::endl;
}
void func(int arg) {}
int main()
{
int dummy(1);
std::cout << measure<>::execution( [&]() {
func(dummy);
}) << std::endl;
std::cout << measure<>::execution(functor(dummy)) << std::endl;
std::cout << measure<>::execution(func);
return 0;
}
But since I started using my struct in various posts on Stack Exchange sites, I've had remarks and comments about the code (like on func
being a const ref or not etc).
I'd like a review on the above and your suggestions on how I can approve this code (or leave it alone at last).