fork download
  1. #include <iostream>
  2. #include <list>
  3. #include <functional>
  4. #include <type_traits>
  5.  
  6. using namespace std;
  7.  
  8. #include <chrono>
  9. #include <iostream>
  10. #include <type_traits>
  11. #include <utility>
  12.  
  13. // Executes fn with arguments args and returns the time needed
  14. // and the result of f if it is not void
  15. template <class R, class... Args>
  16. auto timer(R (*fn)(Args...), Args... args)
  17. -> std::pair<double, R> {
  18. static_assert(!std::is_void<decltype(fn(args...))>::value,
  19. "Call timer_void if return type is void!");
  20. auto start = std::chrono::high_resolution_clock::now();
  21. auto ret = fn(args...);
  22. auto end = std::chrono::high_resolution_clock::now();
  23. std::chrono::duration<double> elapsed_seconds = end - start;
  24. return { elapsed_seconds.count(), ret };
  25. }
  26.  
  27. // If fn returns void, only the time is returned
  28. template < class... Args>
  29. auto timer(void (*fn)(Args...), Args... args)
  30. -> double {
  31. static_assert(std::is_void<decltype(fn(args...))>::value,
  32. "Call timer for non void return type");
  33. auto start = std::chrono::high_resolution_clock::now();
  34. fn(args...);
  35. auto end = std::chrono::high_resolution_clock::now();
  36. std::chrono::duration<double> elapsed_seconds = end - start;
  37. return elapsed_seconds.count();
  38. }
  39.  
  40. int func1()
  41. {
  42. cout << "F1" << endl;
  43. }
  44.  
  45. void func2()
  46. {
  47. cout << "F2" << endl;
  48. }
  49.  
  50. int main () {
  51.  
  52. timer(func1);
  53.  
  54. timer(func2);
  55.  
  56. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
F1
F2