Advertisement
Art_Uspen

Untitled

Mar 1st, 2021
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <string_view>
  5.  
  6. class TimerGuard {
  7. private:
  8.     std::string message_;
  9.     std::ostream &out_;
  10.     using Clock = std::chrono::high_resolution_clock;
  11.     std::chrono::time_point<Clock> start_;
  12. public:
  13.     TimerGuard(std::string_view message = "",
  14.                std::ostream &out = std::cout) : message_(message),
  15.                                                 out_(out),
  16.                                                 start_(Clock::now()) {
  17.         out_ << "{" << message_ << "}" << " ";
  18.     }
  19.  
  20.     ~TimerGuard() {
  21.         std::chrono::duration<double> diff = std::chrono::high_resolution_clock::now() - start_;
  22.         out_ << "{" <<
  23.              diff.count() <<
  24.              "}";
  25.     }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement