forked from 1flei/obstacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_timer.h
133 lines (120 loc) · 3.72 KB
/
my_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
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
#pragma once
#include <map>
#include <string>
#include <chrono>
#include <iostream>
#include <stack>
class MyTimer
{
public:
typedef std::chrono::duration<double> t_type;
MyTimer(const std::string &name) :name(name), isRunning(true){
start();
};
// MyTimer(const char *name) :name(name), isRunning(true){
// start();
// };
~MyTimer() {
end();
};
void start() {
startTime = std::chrono::system_clock::now();
isRunning = true;
}
void end() {
if(isRunning){
endTime = std::chrono::system_clock::now();
t_type t = endTime - startTime;
// printf(" %s: %f\n", name.c_str(), t.count());
if(name != ""){
_tmMap()[name] += t;
_tmMapCnt()[name]++;
}
isRunning = false;
}
}
double getTime(){
return (endTime-startTime).count();
}
template<typename F, typename... Args>
static t_type funcTime(F&& func, Args&&... args){
auto t0 = std::chrono::system_clock::now();
func(std::forward<Args>(args)...);
auto t1 = std::chrono::system_clock::now();
return t_type(t1-t0);
}
static void pusht() {
auto t = std::chrono::system_clock::now();
return get_tstack().push(t);
}
static double popt() {
auto t = get_tstack().top();
get_tstack().pop();
auto duration = std::chrono::duration_cast<t_type>(std::chrono::system_clock::now() - t);
return duration.count();
}
static void clear() {
_tmMap().clear();
_tmMapCnt().clear();
}
static void clear(const std::string &name) {
_tmMap()[name] = t_type::zero();
}
static double get(const std::string &name) {
return _tmMap()[name].count();
}
static std::stack< std::chrono::time_point<std::chrono::system_clock> >& get_tstack()
{
static std::stack< std::chrono::time_point<std::chrono::system_clock> > _tstack;
return _tstack;
}
// static double get(const char *name) {
// //std::string n(name);
// return g_tmMap[std::string(name)].count();
// }
// static int cnt(const char *name) {
// return g_tmMapCnt[std::string(name)];
// }
static int cnt(const std::string &name) {
return _tmMapCnt()[std::string(name)];
}
// static void print(const char *name, std::ostream &outp = std::cout) {
// outp << "Time for " << name << ": " << get(name) << " avg:" << get(name) / cnt(name) << std::endl;
// }
static void print(const std::string &name, std::ostream &outp = std::cout) {
outp << "Time for " << name << ": " << get(name) << " avg:" << get(name) / cnt(name) << " cnt:" << cnt(name) << std::endl;
}
static void printAll(std::ostream &outp = std::cout) {
for (auto it = _tmMap().begin(); it != _tmMap().end(); it++) {
print(it->first, outp);
}
clear();
}
static void printCurTime(std::ostream &outp = std::cout){
auto time_point = std::chrono::system_clock::now();
std::time_t ttp = std::chrono::system_clock::to_time_t(time_point);
outp << ttp << std::endl;
}
template<typename F, typename ...Args>
static double measure(F func, Args&&... args) {
auto start = std::chrono::system_clock::now();
func(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast<t_type>(std::chrono::system_clock::now() - start);
return duration.count();
}
private:
static std::map<std::string, t_type> &_tmMap(){
static std::map<std::string, t_type> g_tmMap;
return g_tmMap;
}
static std::map<std::string, int> &_tmMapCnt(){
static std::map<std::string, int> g_tmMapCnt;
return g_tmMapCnt;
}
// static std::map<std::string, t_type> g_tmMap;
// static std::map<std::string, int> g_tmMapCnt;
std::chrono::time_point<std::chrono::system_clock> startTime;
std::chrono::time_point<std::chrono::system_clock> endTime;
std::string name;
bool isRunning;
};