-
Notifications
You must be signed in to change notification settings - Fork 0
/
timez.hpp
175 lines (152 loc) · 4.78 KB
/
timez.hpp
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
#pragma once
#include <iostream>
#include <string>
#include <chrono>
#include <algorithm>
#include <math.h>
#include <vector>
#include <sstream>
struct timez
{
struct scope
{
scope *header;
std::vector<scope *> sub;
int layer = 0;
const std::string ID = "";
int64_t totalTime = 0;
int64_t runs = 0;
float percent;
scope(const std::string ID, scope *header) : ID(ID), header(header) {}
~scope()
{
for (int i = 0; i < sub.size(); ++i)
{
delete sub[i];
}
}
void recalc()
{
if (sub.size() > 0)
{
int64_t sum = 0;
for (scope *s : sub)
{
s->recalc();
sum += s->totalTime;
}
totalTime = sum;
}
}
void print(std::vector<std::string> &hide)
{
if (sub.size() > 0)
{
std::vector<scope *> temp;
temp.reserve(sub.size());
for (int i = 0; i < sub.size();)
{
for (int h = 0; h < hide.size(); ++h)
{
if (hide[h] == sub[i]->ID)
{
goto Continue;
}
}
temp.push_back(sub[i]);
Continue:
++i;
}
sub.swap(temp);
int64_t overallTime = 0LL;
for (int i = 0; i < sub.size(); ++i)
{
overallTime += sub[i]->totalTime;
}
for (int i = 0; i < sub.size(); ++i)
{
sub[i]->percent = (float)(sub[i]->totalTime * 10000LL / overallTime) * 0.01f;
}
std::sort(sub.begin(), sub.end(), [](const scope *a, const scope *b)
{ return a->percent > b->percent; });
for (int i = 0; i < sub.size(); ++i)
{
std::stringstream tree;
for (int l = 2; l < sub[i]->layer; ++l)
tree << ' ';
if (sub[i]->layer > 1)
tree << (char)192;
tree << ceilf(sub[i]->percent * 100.0f) * 0.01f;
std::stringstream spaces;
for (int l = 1; l < sub[i]->layer; ++l)
spaces << " ";
spaces << sub[i]->ID;
printf("%-15s%-15lld%-16lld%-15lld%s\n", tree.str().c_str(), sub[i]->totalTime / sub[i]->runs, sub[i]->totalTime, sub[i]->runs, spaces.str().c_str());
sub[i]->print(hide);
}
}
}
};
inline static scope *current; // (= Main header)
public:
class perf
{
std::chrono::time_point<std::chrono::high_resolution_clock> startTimepoint;
scope *local = nullptr;
bool stopped = false;
public:
perf(const std::string id)
{
#ifndef RELEASE
for (int i = 0; i < current->sub.size(); ++i)
{
if (current->sub[i]->ID == id)
{
local = current->sub[i];
current = local;
goto Continue;
}
}
local = new scope(id, current);
current = local;
local->layer = local->header->layer + 1;
local->header->sub.push_back(local);
Continue:
startTimepoint = std::chrono::high_resolution_clock::now();
#endif
}
~perf()
{
#ifndef RELEASE
auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch().count();
auto start = std::chrono::time_point_cast<std::chrono::microseconds>(startTimepoint).time_since_epoch().count();
if (!stopped)
{
stopped = true;
local->totalTime += end - start;
++local->runs;
current = local->header;
}
#endif
}
};
static void init()
{
current = new scope("", nullptr);
}
static void print(bool recalcTotal = false, std::vector<std::string> hide = std::vector<std::string>())
{
#ifndef RELEASE
printf(" --- Performance ---\n");
printf(" %% Time(us) Total(us) Used ID\n");
if (recalcTotal)
current->recalc();
current->print(hide);
printf("\n");
#endif
}
static void clean()
{
delete current;
}
};