1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #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(); } };
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; }
int main() { std::cout << measure<>::execution([&]() { }) << " ms" << std::endl;
std::cout << measure<>::execution(functor(3)) << std::endl;
std::cout << measure<>::execution(func); }
|