ulfben

C++: Q&D function timer

Nov 26th, 2017
115
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×