-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeTool.h
134 lines (110 loc) · 3.94 KB
/
TimeTool.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
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
133
134
#ifndef TIMETOOL_H
#define TIMETOOL_H
#include <chrono>
#include <future>
#include "Delegate.h"
#define START_TIMER auto start = std::chrono::high_resolution_clock::now();
#define END_TIMER auto end = std::chrono::high_resolution_clock::now(); \
std::cout << "Duration: " \
<< std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() \
<< " microseconds." << std::endl;
namespace Event {
class StopWatch {
public:
enum TimeType {
Microseconds,
Milliseconds,
Seconds,
Minutes,
Hours,
};
void Start() {
m_start = std::chrono::steady_clock::now();
}
double Duration() const {
return static_cast<double>(m_duration);
}
void End() {
m_end = std::chrono::steady_clock::now();
m_duration = std::chrono::duration_cast<std::chrono::microseconds>(m_end - m_start).count();
}
template<typename _T, typename... Args>
std::chrono::microseconds::rep TestFunc(Delegate<_T> func, Args... args) {
Start();
func(std::forward<Args>(args)...);
End();
return Duration();
}
template<typename _T>
std::chrono::microseconds::rep TestFunc(std::function<_T()> func) {
Start();
func();
End();
return Duration();
}
template<typename _T, typename... Args>
std::chrono::microseconds::rep
TestFunctionMultithread(Delegate<_T> func, int numIterations = 1000, Args... args) {
Start();
std::vector<std::future<void>> futures;
futures.reserve(numIterations);
for (int i = 0; i < numIterations; ++i) {
futures.emplace_back(std::async(std::launch::async, [&func, &args...]() {
func(std::forward<Args>(args)...);
}));
}
// 等待所有异步任务完成
for (auto&future: futures) {
future.wait();
}
End();
// 返回平均时间
return Duration() / numIterations;
}
template<typename _T>
std::chrono::microseconds::rep TestFunctionMultithread(std::function<_T()> func, int numIterations = 1000) {
Start();
std::vector<std::future<void>> futures;
futures.reserve(numIterations);
for (int i = 0; i < numIterations; ++i) {
futures.emplace_back(std::async(std::launch::async, func));
}
// 等待所有异步任务完成
for (auto&future: futures) {
future.wait_for(std::chrono::seconds(10));
}
End();
// 返回平均时间
return Duration() / numIterations;
}
private:
std::chrono::steady_clock::time_point m_start;
std::chrono::steady_clock::time_point m_end;
std::chrono::microseconds::rep m_duration{};
};
inline static std::string TimeFormat(std::chrono::microseconds::rep duration) {
using namespace std::chrono;
auto ms = duration_cast<milliseconds>(microseconds(duration)).count();
auto s = duration_cast<seconds>(microseconds(duration)).count();
auto min = duration_cast<minutes>(microseconds(duration)).count();
auto hr = duration_cast<hours>(microseconds(duration)).count();
std::string result;
if (hr > 0) {
result += std::to_string(hr) + "hr ";
}
if (min > 0) {
result += std::to_string(min % 60) + "min ";
}
if (s > 0) {
result += std::to_string(s % 60) + "s ";
}
if (ms > 0) {
result += std::to_string(ms % 1000) + "ms ";
}
else {
result += std::to_string(duration) + "us";
}
return result.empty() ? "0us" : result;
}
}
#endif //TIMETOOL_H