geekbrains_cpp_difficult_mo.../lesson_2/Timer.hpp

38 lines
805 B
C++
Raw Normal View History

2021-09-26 23:29:11 +00:00
/*
* Timer.hpp
*
* Created on: 26 сент. 2021 г.
* Author: alexander
*/
#pragma once
#include <iostream>
#include <chrono>
class Timer
{
private:
using clock_t = std::chrono::high_resolution_clock;
using second_t = std::chrono::duration<double, std::ratio<1> >;
std::string m_name;
std::chrono::time_point<clock_t> m_beg;
double elapsed() const
{
return std::chrono::duration_cast<second_t>(clock_t::now() -m_beg).count();
}
public:
Timer() : m_beg(clock_t::now()) { }
Timer(std::string name) : m_name(name), m_beg(clock_t::now()) { }
void start(std::string name) {
m_name = name;
m_beg = clock_t::now();
}
void print() const {
std::cout << m_name << ":\t" << elapsed() * 1000 << " ms" << '\n';
}
};