Advertisement
awsmpshk

class Timer

Oct 6th, 2021
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <ctime>
  4. #include <chrono>
  5. #include <cmath>
  6.  
  7. class Timer {
  8. private:
  9.     std::chrono::time_point<std::chrono::system_clock> m_StartTime;
  10.     std::chrono::time_point<std::chrono::system_clock> m_EndTime;
  11.     bool m_bRunning = false;
  12.  
  13. public:
  14.     void start() {
  15.         this->m_StartTime = std::chrono::system_clock::now();
  16.         this->m_bRunning = true;
  17.     }
  18.  
  19.     void end() {
  20.         this->m_EndTime = std::chrono::system_clock::now();
  21.         this->m_bRunning = false;
  22.     }
  23.  
  24.     double elapsedMilliseconds() {
  25.         std::chrono::time_point<std::chrono::system_clock> endTime;
  26.  
  27.         if (this->m_bRunning) {
  28.             endTime = std::chrono::system_clock::now();
  29.         }
  30.         else {
  31.             endTime = this->m_EndTime;
  32.         }
  33.  
  34.         return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - m_StartTime).count();
  35.     }
  36.  
  37.     double elapsedSeconds() {
  38.         return this->elapsedMilliseconds() / 1000.0;
  39.     }
  40. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement