Repository navigation
Expand file tree
/
Copy pathtime.cpp
More file actions
103 lines (87 loc) · 2.48 KB
/
Copy pathtime.cpp
File metadata and controls
103 lines (87 loc) · 2.48 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
/*
by korenkonder
GitHub/GitLab: korenkonder
*/
#include "time.hpp"
static double_t time_struct_get_freq();
time_struct::time_struct() : timestamp() {
get_timestamp();
inv_freq = time_struct_get_freq();
}
double_t time_struct::calc_time() {
#ifdef _WIN32
LARGE_INTEGER timestamp;
if (!QueryPerformanceCounter(×tamp))
timestamp.QuadPart = 0;
return (double_t)(timestamp.QuadPart - this->timestamp.QuadPart) * inv_freq;
#else
clock_t timestamp = clock();
if (timestamp == -1)
timestamp = 0;
return (double_t)(timestamp - this->timestamp) * inv_freq;
#endif
}
double_t time_struct::calc_time_get_timestamp() {
#ifdef _WIN32
LARGE_INTEGER timestamp;
if (!QueryPerformanceCounter(×tamp))
timestamp.QuadPart = 0;
double_t time = (double_t)(timestamp.QuadPart - this->timestamp.QuadPart) * inv_freq;
this->timestamp = timestamp;
return time;
#else
clock_t timestamp = clock();
if (timestamp == -1)
timestamp = 0;
double_t time = (double_t)(timestamp - this->timestamp) * inv_freq;
this->timestamp = timestamp;
return time;
#endif
}
int64_t time_struct::calc_time_int() {
#ifdef _WIN32
LARGE_INTEGER timestamp;
if (!QueryPerformanceCounter(×tamp))
timestamp.QuadPart = 0;
return (int64_t)((double_t)(timestamp.QuadPart - this->timestamp.QuadPart) * inv_freq * 1000.0);
#else
return (int64_t)((double_t)(timestamp - this->timestamp) * inv_freq * 1000.0);
#endif
}
int64_t time_struct::calc_time_int_get_timestamp() {
#ifdef _WIN32
LARGE_INTEGER timestamp;
if (!QueryPerformanceCounter(×tamp))
timestamp.QuadPart = 0;
int64_t time = (int64_t)((double_t)(timestamp.QuadPart - this->timestamp.QuadPart) * inv_freq * 1000.0);
this->timestamp = timestamp;
return time;
#else
clock_t timestamp = clock();
if (timestamp == -1)
timestamp = 0;
int64_t time = (int64_t)((double_t)(timestamp - this->timestamp) * inv_freq * 1000.0);
this->timestamp = timestamp;
return time;
#endif
}
void time_struct::get_timestamp() {
#ifdef _WIN32
if (!QueryPerformanceCounter(×tamp))
timestamp.QuadPart = 0;
#else
timestamp = clock();
if (timestamp == -1)
timestamp = 0;
#endif
}
static double_t time_struct_get_freq() {
#ifdef _WIN32
LARGE_INTEGER freq;
if (QueryPerformanceFrequency(&freq))
return 1000.0 / (double_t)freq.LowPart;
return 0.0;
#else
return 1000.0 / CLOCKS_PER_SEC;
#endif
}