nochnoikoshmar1

Untitled

May 8th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <chrono>
  4. #include <iostream>
  5.  
  6. #define PROFILE_CONCAT_INTERNAL(X, Y) X##Y
  7. #define PROFILE_CONCAT(X, Y) PROFILE_CONCAT_INTERNAL(X, Y)
  8. #define UNIQUE_VAR_NAME_PROFILE PROFILE_CONCAT(profileGuard, __LINE__)
  9. #define LOG_DURATION(x) LogDuration UNIQUE_VAR_NAME_PROFILE(x)
  10.  
  11. class LogDuration {
  12. public:
  13.     // заменим имя типа std::chrono::steady_clock
  14.     // с помощью using для удобства
  15.     using Clock = std::chrono::steady_clock;
  16.  
  17.     LogDuration(const std::string& id) : id_(id) {
  18.     }
  19.  
  20.     ~LogDuration() {
  21.         using namespace std::chrono;
  22.         using namespace std::literals;
  23.  
  24.         const auto end_time = Clock::now();
  25.         const auto dur = end_time - start_time_;
  26.         std::cerr << id_ << ": "s << duration_cast<milliseconds>(dur).count() << " ms"s << std::endl;
  27.     }
  28.  
  29. private:
  30.     const std::string id_;
  31.     const Clock::time_point start_time_ = Clock::now();
  32. };
Add Comment
Please, Sign In to add comment