SHARE
TWEET

C++: Q&D function timer

ulfben Nov 26th, 2017 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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";
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top