-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_timer.h
More file actions
165 lines (133 loc) · 5.85 KB
/
Copy pathexec_timer.h
File metadata and controls
165 lines (133 loc) · 5.85 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
/*
* Name: ExecTimer
* Description: measures execution time of code. Required C++ 11 compiler at least.
* License: GNU GPL 2.1
* Contact: mmancuska@gmail.com
*/
#ifndef __EXEC_TIMER_H__
#define __EXEC_TIMER_H__
#include <iostream>
#include <iomanip>
#include <fstream>
#include <map>
#include <string>
#include <chrono>
#include <algorithm>
using std::string;
using std::map;
using std::ofstream;
using std::ifstream;
// msvc __func__ fix
#ifndef __func__
#define __func__ __FUNCTION__
#endif
namespace met {
/* Main class for measuring time of executed code */
class ExecTimer {
private:
/* begin and end points of measuring */
struct StartEnd {
std::chrono::time_point<std::chrono::high_resolution_clock> start;
std::chrono::time_point<std::chrono::high_resolution_clock> end;
StartEnd() : start(), end() { }
};
string mName;
map<const string, StartEnd> mSections;
public:
ExecTimer() : mName("exec_timer") { }
explicit ExecTimer(const string& name) : mName(name) {
}
~ExecTimer() { }
ExecTimer(const ExecTimer& et) = delete;
ExecTimer& operator=(const ExecTimer& et) = delete;
void start(const string& label) {
StartEnd se;
mSections.insert(std::pair<const string, StartEnd> (label, se));
StartEnd& seStart = mSections.at(label);
seStart.start = std::chrono::high_resolution_clock::now();
}
void stop(const string& label) {
std::chrono::time_point<std::chrono::high_resolution_clock> endNow = std::chrono::high_resolution_clock::now();
StartEnd& se = mSections.at(label);
se.end = endNow;
}
void print() {
std::cout << std::left << std::setw(30) << "Section" << std::setw(20) << "Time (microseconds)" << std::endl;
std::cout << std::left << std::setw(30) << "-------" << std::setw(20) << "-------------------" << std::endl;
for (auto& section : mSections) {
auto diff = std::chrono::duration_cast<std::chrono::microseconds> (section.second.end - section.second.start).count();
std::cout << std::left << std::setw(30) << section.first << std::setw(20) << diff << std::endl;
}
}
void save() {
string filename(mName);
filename.append(".log");
std::replace_if(filename.begin(), filename.end(), [](const auto& c) {
if (c == ':')
return true;
return false;
}, '_');
auto outputExists = [] (const string& name) -> bool {
ifstream f(name);
return f.good();
};
bool exists = outputExists(mName);
ofstream out(filename, std::ios::out | std::ios::ate);
if (!exists) {
out << std::left << std::setw(30) << "Section" << std::setw(20) << "Time (microseconds)" << std::endl;
out << std::left << std::setw(30) << "-------" << std::setw(20) << "-------------------" << std::endl;
}
for (auto& section : mSections) {
auto diff = std::chrono::duration_cast<std::chrono::microseconds> (section.second.end - section.second.start).count();
out << std::left << std::setw(30) << section.first << std::setw(20) << diff << std::endl;
}
}
};
/* auto measuring of given scope */
class ExecTimerAuto {
private:
ExecTimer mET;
string mLabel;
bool mSave {false};
public:
explicit ExecTimerAuto(const string& label, bool save = false) : mET(label), mLabel(label), mSave (save)
{
mET.start(mLabel);
}
ExecTimerAuto(const string& name, const string& label) : mET(name), mLabel(label)
{
mET.start(mLabel);
}
~ExecTimerAuto() {
mET.stop(mLabel);
if (mSave)
mET.save();
else
mET.print();
}
ExecTimerAuto(const ExecTimerAuto& eta) = delete;
ExecTimerAuto& operator=(const ExecTimerAuto& eta) = delete;
};
/* helper scope class */
class ExecTimerScope {
private:
ExecTimer& mEt;
string mLabel;
public:
ExecTimerScope(ExecTimer& et, const string& label) : mEt(et), mLabel(label) { mEt.start(mLabel); }
ExecTimerScope(ExecTimer& et, const string& label, const int idx) : mEt(et), mLabel(label + std::to_string(idx)) { mEt.start(mLabel); }
~ExecTimerScope() { mEt.stop(mLabel); }
};
}
#define MET_AUTO_BENCHMARK_FUNCTION met::ExecTimerAuto autoBenchETA(__func__);
#define MET_AUTO_BENCHMARK_FUNCTION_SAVE met::ExecTimerAuto autoBenchETA(__func__, true);
#define MET_BENCHMARK_CREATE(NAME) met::ExecTimer NAME(#NAME);
#define MET_BENCHMARK_START(NAME, SECTION) NAME.start(#SECTION);
#define MET_BENCHMARK_STOP(NAME, SECTION) NAME.stop(#SECTION);
#define MET_BENCHMARK_PRINT(NAME) NAME.print();
#define MET_BENCHMARK_SAVE(NAME) NAME.save();
#define MET_BENCHMARK_SCOPE(NAME, SECTION) met::ExecTimerScope scopedEt(NAME, #SECTION);
#define MET_BENCHMARK_LOOP(NAME, SECTION) static int SECTION##_idx = 0; \
met::ExecTimerScope SECTION##_scopedEt(NAME, #SECTION, SECTION##_idx); \
++SECTION##_idx; \
#endif