-
-
Save Eren121/d4139912bd2c62805985ac5c684aa817 to your computer and use it in GitHub Desktop.
Timer class for C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <chrono> | |
#include <string> | |
#include <iostream> | |
class Timer { | |
public: | |
Timer(const std::string& title = "") : m_name(title), m_begin(m_clock::now()) {} | |
void print() { | |
std::cout << m_name << " elapsed: " << elapsed() << "s" << std::endl; | |
} | |
void reset() { | |
m_begin = m_clock::now(); | |
} | |
double elapsed() const { | |
return std::chrono::duration_cast<m_seconds>(m_clock::now() - m_begin).count(); | |
} | |
private: | |
std::string m_name; | |
typedef std::chrono::high_resolution_clock m_clock; | |
typedef std::chrono::duration<double, std::ratio<1> > m_seconds; | |
std::chrono::time_point<m_clock> m_begin; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment