Skip to content

Instantly share code, notes, and snippets.

@Eren121
Forked from svdamani/Timer.hpp
Last active July 1, 2024 12:03
Show Gist options
  • Save Eren121/d4139912bd2c62805985ac5c684aa817 to your computer and use it in GitHub Desktop.
Save Eren121/d4139912bd2c62805985ac5c684aa817 to your computer and use it in GitHub Desktop.
Timer class for C++
#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