-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeTimer.hpp
More file actions
132 lines (110 loc) · 2.9 KB
/
CodeTimer.hpp
File metadata and controls
132 lines (110 loc) · 2.9 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#pragma once
#include <chrono>
#include <thread>
class CodeTimer
{
public:
std::chrono::steady_clock::time_point tpStart{};
std::chrono::steady_clock::time_point tpStop{};
enum UnitConversion : int64_t
{
nanos_per_micro = 1000, //1000ns=1us
micros_per_milli = 1000, //1000us=1ms
millis_per_second = 1000, //1000ms=1s
seconds_per_minute = 60, //60s=1min
minutes_per_hour = 60, //60min=1h
};
static inline constexpr UnitConversion arrConv[] =//用于遍历比较并进行单位换算
{
nanos_per_micro,
micros_per_milli,
millis_per_second,
seconds_per_minute,
minutes_per_hour,
};
enum UnitType : uint8_t
{
nano = 0, //ns
micro, //us
milli, //ms
second, //s
minute, //min
hour, //h
unknown, //unknown
UnitType_END,//end flag
};
static inline constexpr const char *const strUnitType[] =
{
"ns",
"us",
"ms",
"s",
"min",
"h",
"unknown"
};
static_assert(sizeof(strUnitType) / sizeof(strUnitType[0]) == UnitType_END, "Array and enumeration lose synchronization");
struct OutputData
{
UnitType enUnitType = nano;
long double ldTime = 0.0;
};
OutputData GetOutputData(int64_t i64Nano)
{
OutputData ret{ .enUnitType = nano,.ldTime = (long double)i64Nano };
for (const auto &it : arrConv)
{
if (ret.ldTime < (long double)it)
{
break;//如果当前在下一个单位范围内,则跳出
}
//否则切换并计算下一个单位
ret.enUnitType = (UnitType)((uint8_t)ret.enUnitType + 1);
ret.ldTime /= (long double)it;
}
//超出范围,设置为未知
if (ret.enUnitType >= UnitType_END)
{
ret.enUnitType = unknown;
}
return ret;
}
public:
CodeTimer(void) = default;
~CodeTimer(void) = default;
void Start(void)
{
tpStart = std::chrono::steady_clock::now();
}
void Stop(void)
{
tpStop = std::chrono::steady_clock::now();
}
template<typename T = std::chrono::nanoseconds>
T Diff(void)
{
return std::chrono::duration_cast<T>(tpStop - tpStart);
}
//打印时差
void PrintElapsed(const char *const cpBegInfo = "", const char *const cpEndInfo = "\n")
{
//通过时差长度选择合适的单位
auto [enUnitType, ldTime] = GetOutputData(Diff<std::chrono::nanoseconds>().count());
printf("%s%.6Lf%s%s", cpBegInfo, ldTime, strUnitType[enUnitType], cpEndInfo);
}
template<typename T = std::chrono::milliseconds>
static uint64_t GetSteadyTime(void)
{
return std::chrono::duration_cast<T>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
template<typename T = std::chrono::milliseconds>
static uint64_t GetSystemTime(void)
{
return std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch()).count();
}
template<typename T = std::chrono::milliseconds>
static void Sleep(const T &t)
{
return std::this_thread::sleep_for(t);
}
};