-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeDebugger.h
58 lines (54 loc) · 1.23 KB
/
TimeDebugger.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
#pragma once
#include <iostream>
#include <ostream>
#include <chrono>
namespace labbish::message {
class TimeMsg :public std::ostream {
public:
inline TimeMsg() :std::ostream(&buf) {
buf.setColor();
}
inline ~TimeMsg() {
buf.resetColor();
}
private:
class TimeBuf : public std::streambuf {
public:
inline void setColor() {
std::clog << "\033[32m[TIME] Command Time Cost: ";
}
inline void resetColor() {
std::clog << "ms. \033[0m\n";
}
inline int overflow(int c) override {
if (c != EOF) {
std::clog.put(c);
}
return c;
}
};
TimeBuf buf;
};
class TimeDebugger {
std::chrono::high_resolution_clock::time_point start, end;
double cost;
public:
inline TimeDebugger() {
start = end = std::chrono::high_resolution_clock::now();
cost = 0;
}
inline void flush() {
start = end = std::chrono::high_resolution_clock::now();
cost = 0;
}
inline void debug() {
end = std::chrono::high_resolution_clock::now();
cost = (double)std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000;
TimeMsg() << cost;
start = end;
}
inline double previousCost() {
return cost;
}
};
}