-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.cpp
47 lines (39 loc) · 1.16 KB
/
clock.cpp
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
// clock.cpp
#include "clock.h"
Clock::Clock() {
T0 = std::chrono::high_resolution_clock::now();
}
void Clock::reset() {
intervals.clear();
}
void Clock::start() {
t0 = std::chrono::high_resolution_clock::now();
}
void Clock::stop() {
auto t = std::chrono::high_resolution_clock::now();
auto dt = std::chrono::duration<double>(t - t0).count();
intervals.push_back(dt);
}
double Clock::get_runtime() const {
auto T = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double>(T - T0).count();
}
void Clock::write_stat(const string& fname) const {
std::ofstream f;
f.open(fname);
f << "itrN," << intervals.size()
<< "\nitrT," << intervals.back()
<< "\navgT," << std::accumulate( intervals.begin(), intervals.end(), 0.0 ) / intervals.size()
<< "\nmaxT," << *std::max_element( intervals.begin(), intervals.end() )
<< "\nrunT," << this->get_runtime()
<< endl;
f.close();
}
void Clock::write_data(const string& fname) const {
std::ofstream f;
f.open(fname);
for (const auto& t : intervals) {
f << t << "\n";
}
f.close();
}