Advertisement
ulfben

C++: Q&D function timer

Nov 26th, 2017
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. template<typename Time = std::chrono::microseconds,
  2.     typename Clock = std::chrono::high_resolution_clock>
  3.     struct QuickTimer{
  4.     template<typename F, typename... Args>
  5.     static Time duration(F&& f, Args... args){
  6.         auto start = Clock::now();
  7.         std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
  8.         auto end = Clock::now();
  9.         return std::chrono::duration_cast<Time>(end - start);
  10.     }
  11. };
  12.  
  13. //Usage:
  14. using namespace std::chrono;
  15. auto t = QuickTimer<>::duration(myFunction);
  16. std::cout << duration<double, std::milli>(t).count() << "ms\n";
  17. std::cout << duration<double, std::nano>(t).count() << "ns\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement