-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhigh_resolution_timer.h
More file actions
203 lines (183 loc) · 6.74 KB
/
high_resolution_timer.h
File metadata and controls
203 lines (183 loc) · 6.74 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
///////////////////////////////////////////////////////////////////////////////////////////////
/// @file HighResolutionTimer.h
/// @brief 定义并实现一个高精度时钟,可提供秒,毫秒及微秒级的计时精度
/// @author Veidt provides the Window version
/// @date 20150414
/// @author Aimin provides the Linux and MacOS version and Other version
/// @date 20160719
///
/// class HighResolutionTimer{
/// void start();
/// double seconds_elapsed() ;
/// double milliseconds_elapsed();
/// double microseconds_elapsed();
/// }
///每次计时开始,调用该对象的start函数,计时结束时:
///调用seconds_elapsed函数获取以从上一次start开始以秒为单位的时间消耗
///调用milliseconds_elapsed函数获取以从上一次start开始以毫秒为单位的时间消耗(ms=1/1000 s)
///调用microseconds_elapsed函数获取以从上一次start开始以微秒为单位的时间消耗(us=1/1000 ms = 1/10^6 s)
///////////////////////////////////////////////////////////////////////////////////////////////
#ifndef HIGH_RESOLUTION_TIMER_H
#define HIGH_RESOLUTION_TIMER_H
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(__TOS_WIN__) || defined(__WINDOWS__)
#include <Windows.h>
class HighResolutionTimer
{
public:
const static char version = 'W'; ///Windows
public:
HighResolutionTimer(){
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
high_res_frequency_ = (double)frequency.QuadPart;
QueryPerformanceCounter(&start_time_);
}
inline void start(){
QueryPerformanceCounter(&start_time_);
}
inline double seconds_elapsed() {
QueryPerformanceCounter(&end_time_);
return (end_time_.QuadPart - start_time_.QuadPart)/high_res_frequency_;
}
inline double milliseconds_elapsed() {
QueryPerformanceCounter(&end_time_);
return 1000*(end_time_.QuadPart - start_time_.QuadPart)/high_res_frequency_;
}
inline double microseconds_elapsed() {
QueryPerformanceCounter(&end_time_);
return 1000000*(end_time_.QuadPart - start_time_.QuadPart)/high_res_frequency_;
}
private:
LARGE_INTEGER start_time_;
LARGE_INTEGER end_time_;
double high_res_frequency_;
};
#elif defined(linux) || defined(__linux)
#include <time.h>
/// CLOCK_REALTIME, a system-wide realtime clock.
/// CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
/// CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.
class HighResolutionTimer
{
public:
const static char version = 'L'; ///Linux
public:
HighResolutionTimer(const clockid_t clk_id = CLOCK_REALTIME )
:clk_id_(clk_id){
clock_gettime(clk_id_, &start_time_);
}
inline void start(){
clock_gettime(clk_id_, &start_time_);
}
inline double seconds_elapsed() {
clock_gettime(clk_id_, &end_time_);
return (end_time_.tv_sec - start_time_.tv_sec) + (end_time_.tv_nsec - start_time_.tv_nsec)/1000000000.0;
}
double milliseconds_elapsed() {
clock_gettime(clk_id_, &end_time_);
return (end_time_.tv_sec - start_time_.tv_sec) * 1000.0 + (end_time_.tv_nsec - start_time_.tv_nsec)/1000000.0;
}
inline double microseconds_elapsed() {
clock_gettime(clk_id_, &end_time_);
return (end_time_.tv_sec - start_time_.tv_sec) * 1000000.0 + (end_time_.tv_nsec - start_time_.tv_nsec)/1000.0;
}
private:
const clockid_t clk_id_;
timespec start_time_;
timespec end_time_;
};
#elif defined(macintosh) || defined(Macintosh) || (defined(__APPLE__) && defined(__MACH__)) /////for MAC OS in my case
#include <sys/time.h>
#include <mach/clock.h>
#include <mach/mach.h>
#ifndef _CLOCKID_T
#define _CLOCKID_T
typedef int clockid_t; /* clock identifier type */
#endif /* ifndef _CLOCKID_T */
#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC ((clockid_t)0)
/* system-wide monotonic clock (aka system time) */
#endif
#ifndef CLOCK_REALTIME
#define CLOCK_REALTIME ((clockid_t)-1)
/* system-wide real time clock */
#endif
#ifndef CLOCK_PROCESS_CPUTIME_ID
#define CLOCK_PROCESS_CPUTIME_ID ((clockid_t)-2)
/* clock measuring the used CPU time of the current process */
#endif
#ifndef CLOCK_THREAD_CPUTIME_ID
#define CLOCK_THREAD_CPUTIME_ID ((clockid_t)-3)
/* clock measuring the used CPU time of the current thread */
#endif
class HighResolutionTimer
{
public:
const static char version = 'M'; ///MAC
public:
HighResolutionTimer(const clockid_t clk_id = CLOCK_REALTIME )
:clock_service_(clock_serv_t())
{
/// if( clk_id == CLOCK_REALTIME)
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &clock_service_);
/// else
/// host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &clock_service_);
/// clock_serv_t cclock;
/// mach_timespec_t mts;
clock_get_time(clock_service_, &start_time_);
/// mach_port_deallocate(mach_task_self(), cclock);
}
~HighResolutionTimer(){
mach_port_deallocate(mach_task_self(), clock_service_);
}
inline void start(){
clock_get_time(clock_service_, &start_time_);
}
inline double seconds_elapsed() {
clock_get_time(clock_service_, &end_time_);
return (end_time_.tv_sec - start_time_.tv_sec) + (end_time_.tv_nsec - start_time_.tv_nsec) / 1000000000.0;
}
inline double milliseconds_elapsed() {
clock_get_time(clock_service_, &end_time_);
return (end_time_.tv_sec - start_time_.tv_sec) * 1000.0 + (end_time_.tv_nsec - start_time_.tv_nsec) / 1000000.0;
}
inline double microseconds_elapsed() {
clock_get_time(clock_service_, &end_time_);
return (end_time_.tv_sec - start_time_.tv_sec) * 1000000.0 + (end_time_.tv_nsec - start_time_.tv_nsec)/1000.0;
}
private:
clock_serv_t clock_service_;
mach_timespec_t start_time_;
mach_timespec_t end_time_;
};
#else ////for all other systems
#include <sys/time.h>
class HighResolutionTimer
{
public:
const static char version = 'O'; ///Other
public:
HighResolutionTimer( ) {
gettimeofday(&start_time_, NULL);
}
inline void start(){
gettimeofday(&start_time_, NULL);
}
inline double seconds_elapsed() {
gettimeofday(&end_time_, NULL);
return (end_time_.tv_sec - start_time_.tv_sec) + (end_time_.tv_usec - start_time_.tv_usec) / 1000000.0;
}
inline double milliseconds_elapsed() {
gettimeofday(&end_time_, NULL);
return (end_time_.tv_sec - start_time_.tv_sec) * 1000.0 + (end_time_.tv_usec - start_time_.tv_usec) / 1000.0;
}
inline double microseconds_elapsed() {
gettimeofday(&end_time_, NULL);
return (end_time_.tv_sec - start_time_.tv_sec) * 1000000.0 + (end_time_.tv_usec - start_time_.tv_usec);
}
private:
timeval start_time_;
timeval end_time_;
};
#endif
#endif