Last active
August 11, 2023 07:42
-
-
Save magicianlib/0ed71627bb9b4560967cf889efb01d06 to your computer and use it in GitHub Desktop.
C++ timer计时器
This file contains hidden or 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
#ifndef PROJECT_TIMER_HPP | |
#define PROJECT_TIMER_HPP | |
#include <ratio> | |
#include <chrono> | |
namespace datetime { | |
// DurationPeriod | |
typedef std::chrono::nanoseconds Nano; // 纳秒 <long, long> | |
typedef std::chrono::microseconds Micro; // 微秒 <long, long> | |
typedef std::chrono::milliseconds Milli; // 毫秒 <long, long> | |
typedef std::chrono::seconds Seconds; // 秒 <long, long> | |
typedef std::chrono::minutes Minutes; // 分钟 <long> | |
typedef std::chrono::hours Hours; // 小时 <long> | |
typedef std::chrono::duration<long, std::ratio<60 * 60 * 24>> Day; // 天 | |
typedef std::chrono::duration<long, std::ratio<60 * 60 * 24 * 7>> Week; // 周 | |
typedef std::chrono::duration<long, std::ratio<60 * 60 * 24 * 30>> Month; // 月 | |
// Clock | |
typedef std::chrono::steady_clock SteadyClock; | |
typedef std::chrono::system_clock SystemClock; | |
template<typename DurationPeriod = Milli, typename Clock = SteadyClock> | |
class Timer final { | |
private: | |
typedef DurationPeriod Period; | |
typedef std::chrono::time_point<Clock> TimePoint; | |
TimePoint start{Clock::now()}; | |
public: | |
void reset() { | |
start = Clock::now(); | |
} | |
typename Period::rep elapsed() const { | |
return std::chrono::duration_cast<Period>(Clock::now() - start).count(); | |
} | |
}; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment