-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.h
91 lines (78 loc) · 2.6 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
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
//
// Created by sbian on 2019/9/5.
//
#ifndef BIM_TIMER_H
#define BIM_TIMER_H
#include <chrono>
class Timer
{
private:
std::chrono::high_resolution_clock::time_point __StartTime, __LastTime, __EndTime;
const char* __processName;
public:
Timer()
{
__StartTime = std::chrono::high_resolution_clock::now();
__LastTime = __StartTime, __EndTime = __StartTime;
__processName = "Unnamed";
}
explicit Timer(const char* processName)
{
__StartTime = std::chrono::high_resolution_clock::now();
__LastTime = __StartTime, __EndTime = __StartTime;
__processName = processName;
}
/// Refresh time
void refresh_time()
{
__StartTime = std::chrono::high_resolution_clock::now();
__LastTime = __StartTime, __EndTime = __StartTime;
}
/// Record current time
void record_current_time()
{
__LastTime = __EndTime;
__EndTime = std::chrono::high_resolution_clock::now();
};
/// Get the operation time from the last recorded moment
double get_operation_time()
{
this->record_current_time();
std::chrono::duration<double> elapsed = __EndTime - __LastTime;
return elapsed.count();
}
/// Print the operation time from the last recorded moment
void log_operation_time()
{
const double duration = this->get_operation_time();
std::cout << "->Time used (sec): " << duration << '\n';
}
/// Print the operation time from the last recorded moment with a given name
void log_operation_time(const char* operationName)
{
const double duration = this->get_operation_time();
std::cout << "->Time used (sec) for operation [" << operationName << "]: " << duration << '\n';
}
/// Get the total time from the beginning
double get_total_time()
{
this->record_current_time();
std::chrono::duration<double> elapsed = __EndTime - __StartTime;
return elapsed.count();
}
/// Print the total time from the beginning
void log_total_time()
{
const double duration = this->get_total_time();
std::cout << "--->Time used (sec) for process [" << __processName << "]: " << duration << '\n';
}
/// Print the total time from the beginning to the last recorded moment
void log_sub_total_time() const
{
std::chrono::duration<double> elapsed = __EndTime - __StartTime;
std::cout << "--->Time used (sec) for process [" << __processName << "]: " << elapsed.count() << '\n';
}
};
using TTimer = Timer;
using PTimer = std::shared_ptr<Timer>;
#endif //BIM_TIMER_H