-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.h
59 lines (48 loc) · 1.37 KB
/
Timer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*****************************************************************************
* @file: Timer.h
* @brief:
*
* Copyright (c) 2022 O-Net Communications Inc.
****************************************************************************/
#pragma once
#include <chrono>
namespace cppbase {
using ns = std::chrono::nanoseconds;
using us = std::chrono::microseconds;
using ms = std::chrono::milliseconds;
using s = std::chrono::seconds;
using m = std::chrono::minutes;
using h = std::chrono::hours;
template <typename DurationType>
class Timer;
using TimerNs = Timer<ns>;
using TimerUs = Timer<us>;
using TimerMs = Timer<ms>;
using TimerS = Timer<s>;
using TimerM = Timer<m>;
using TimerH = Timer<h>;
template <typename DurationType = us>
class Timer
{
public:
Timer() : m_started(true), m_last(std::chrono::steady_clock::now()) {}
void Start()
{
m_started = true;
m_last = std::chrono::steady_clock::now();
}
void Stop() { m_started = false; }
bool IsRunning() const { return m_started; }
typename DurationType::rep Elapsed() const
{
if (!m_started)
return 0;
auto elapsed =
std::chrono::duration_cast<DurationType>(std::chrono::steady_clock::now() - m_last);
return elapsed.count();
}
private:
bool m_started;
std::chrono::steady_clock::time_point m_last;
};
} // namespace cppbase