-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathtimer.cpp
More file actions
190 lines (158 loc) · 4.43 KB
/
Copy pathtimer.cpp
File metadata and controls
190 lines (158 loc) · 4.43 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
/*
timer.cpp
Copyright (c) 2014, 2015, 2016 Terumasa Tadano
This file is distributed under the terms of the MIT license.
Please see the file 'LICENCE.txt' in the root directory
or http://opensource.org/licenses/mit-license.php for information.
*/
#include "timer.h"
#include <string>
#include <iostream>
#include <ctime>
using namespace ALM_NS;
Timer::Timer()
{
#if defined(WIN32) || defined(_WIN32)
QueryPerformanceCounter(&walltime_ref);
QueryPerformanceFrequency(&frequency);
cputime_ref = get_cputime();
#else
gettimeofday(&walltime_ref, nullptr);
cputime_ref = static_cast<double>(clock());
#endif
lock = false;
}
Timer::~Timer()
{
walltime.clear();
cputime.clear();
}
void Timer::reset()
{
#if defined(WIN32) || defined(_WIN32)
QueryPerformanceCounter(&walltime_ref);
#else
gettimeofday(&walltime_ref, nullptr);
#endif
}
double Timer::elapsed_walltime() const
{
#if defined(WIN32) || defined(_WIN32)
LARGE_INTEGER time_now;
QueryPerformanceCounter(&time_now);
return static_cast<double>(time_now.QuadPart - walltime_ref.QuadPart)
/ static_cast<double>(frequency.QuadPart);
#else
timeval time_now;
gettimeofday(&time_now, nullptr);
return (time_now.tv_sec - walltime_ref.tv_sec)
+ (time_now.tv_usec - walltime_ref.tv_usec) * 1.0e-6;
#endif
}
#if defined(WIN32) || defined(_WIN32)
double Timer::get_cputime() const
{
FILETIME createTime;
FILETIME exitTime;
FILETIME kernelTime;
FILETIME userTime;
if (GetProcessTimes(GetCurrentProcess(),
&createTime, &exitTime, &kernelTime, &userTime) != -1) {
SYSTEMTIME userSystemTime;
if (FileTimeToSystemTime(&userTime, &userSystemTime) != -1)
return static_cast<double>(userSystemTime.wHour) * 3600.0 +
static_cast<double>(userSystemTime.wMinute) * 60.0 +
static_cast<double>(userSystemTime.wSecond) +
static_cast<double>(userSystemTime.wMilliseconds) / 1000.0;
}
return 0.0;
}
#endif
double Timer::elapsed_cputime() const
{
#if defined(WIN32) || defined(_WIN32)
return get_cputime() - cputime_ref;
#else
return (static_cast<double>(clock()) - cputime_ref) / CLOCKS_PER_SEC;
#endif
}
void Timer::print_elapsed() const
{
std::cout << " Time Elapsed: " << elapsed_walltime() << " sec."
<< std::endl << std::endl;
}
std::string Timer::DateAndTime()
{
time_t current;
std::time(¤t);
#if defined(WIN32) || defined(_WIN32)
struct tm local{};
char str_now[32];
auto err_t = localtime_s(&local, ¤t);
err_t = asctime_s(str_now, 32, &local);
return str_now;
#else
struct tm *local;
local = std::localtime(¤t);
return asctime(local);
#endif
}
void Timer::start_clock(const std::string str_tag)
{
if (lock) {
std::cout << "Error: cannot start clock because it's occupied." << std::endl;
exit(1);
}
// Initialize the counter if the key is new
if (walltime.find(str_tag) == walltime.end()) {
walltime[str_tag] = 0.0;
}
if (cputime.find(str_tag) == cputime.end()) {
cputime[str_tag] = 0.0;
}
wtime_tmp = elapsed_walltime();
ctime_tmp = elapsed_cputime();
lock = true;
}
void Timer::stop_clock(const std::string str_tag)
{
if (!lock) {
std::cout << "Error: cannot stop clock because it's not initialized." << std::endl;
exit(1);
}
auto it = walltime.find(str_tag);
if (it == walltime.end()) {
std::cout << "Error: invalid tag for clock" << std::endl;
exit(1);
}
auto time_tmp = (*it).second;
time_tmp += elapsed_walltime() - wtime_tmp;
walltime[str_tag] = time_tmp;
it = cputime.find(str_tag);
if (it == cputime.end()) {
std::cout << "Error: invalid tag for clock" << std::endl;
exit(1);
}
time_tmp = (*it).second;
time_tmp += elapsed_cputime() - ctime_tmp;
cputime[str_tag] = time_tmp;
lock = false;
}
double Timer::get_walltime(const std::string str_tag)
{
const auto it = walltime.find(str_tag);
if (it == walltime.end()) {
std::cout << "Error: invalid tag for clock" << std::endl;
exit(1);
}
return (*it).second;
}
double Timer::get_cputime(const std::string str_tag)
{
const auto it = cputime.find(str_tag);
if (it == cputime.end()) {
std::cout << "Error: invalid tag for clock" << std::endl;
exit(1);
}
return (*it).second;
}