fork(2) download
  1. #include <iostream>
  2. #include <chrono>
  3.  
  4. using namespace std;
  5.  
  6. template<typename TimeT = std::chrono::milliseconds>
  7. struct measure
  8. {
  9. template<typename F, typename ...Args>
  10. static typename TimeT::rep execution(F func, Args&&... args)
  11. {
  12. auto start = std::chrono::system_clock::now();
  13.  
  14. // Now call the function with all the parameters you need.
  15. func(std::forward<Args>(args)...);
  16.  
  17. auto duration = std::chrono::duration_cast< TimeT>(std::chrono::system_clock::now() - start);
  18.  
  19. return duration.count();
  20. }
  21. };
  22.  
  23. struct functor
  24. {
  25. int state;
  26. functor(int state) : state(state) {}
  27. void operator()() const
  28. {
  29. std::cout << "In functor run for ";
  30. }
  31. };
  32.  
  33. void func()
  34. {
  35. std::cout << "In function, run for " << std::endl;
  36. }
  37.  
  38. void func(int arg) {}
  39.  
  40. int main()
  41. {
  42. int dummy(1);
  43.  
  44. std::cout << measure<>::execution( [&]() {
  45. func(dummy);
  46. }) << std::endl;
  47.  
  48. std::cout << measure<>::execution(functor(dummy)) << std::endl;
  49. std::cout << measure<>::execution(func);
  50.  
  51. return 0;
  52. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:49:39: error: no matching function for call to ‘measure<>::execution(<unresolved overloaded function type>)’
 std::cout << measure<>::execution(func);
                                       ^
prog.cpp:49:39: note: candidate is:
prog.cpp:10:32: note: template<class F, class ... Args> static typename TimeT::rep measure<TimeT>::execution(F, Args&& ...) [with F = F; Args = {Args ...}; TimeT = std::chrono::duration<long long int, std::ratio<1ll, 1000ll> >]
     static typename TimeT::rep execution(F func, Args&&... args)
                                ^
prog.cpp:10:32: note:   template argument deduction/substitution failed:
prog.cpp:49:39: note:   couldn't deduce template parameter ‘F’
 std::cout << measure<>::execution(func);
                                       ^
stdout
Standard output is empty