-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.h
61 lines (56 loc) · 1.27 KB
/
profile.h
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
#ifndef PROFILE_H
#define PROFILE_H
#include <windows.h>
#include <deque>
class Timer{
public:
Timer() {QueryPerformanceFrequency(&frequency);}
void Start() {QueryPerformanceCounter(&start);}
double End() {
QueryPerformanceCounter(&end);
return double(end.QuadPart - start.QuadPart) / frequency.QuadPart;
}
private:
LARGE_INTEGER frequency;
LARGE_INTEGER start;
LARGE_INTEGER end;
};
class Profile{
public:
Profile(int s) : maxt(s) {t = NULL;}
~Profile() {delete t;}
void Start() {if (!t) t = new Timer; t->Start();}
void End() {
if (!t) return;
times.emplace_front(t->End());
delete t;
t = NULL;
if (times.size() > maxt) times.pop_back();
}
double Get() {
double result = 0;
for (int i = 0; i < times.size(); i++)
result += times[i];
return result / times.size();
}
private:
Timer* t;
int maxt;
std::deque<double> times;
};
inline double elapsedTime()
{
static LARGE_INTEGER start;
static LARGE_INTEGER now;
static LARGE_INTEGER freq;
if (freq.QuadPart == 0)
QueryPerformanceFrequency(&freq);
if (start.QuadPart == 0)
{
QueryPerformanceCounter(&start);
return double(start.QuadPart) / freq.QuadPart;
}
QueryPerformanceCounter(&now);
return double(now.QuadPart - start.QuadPart) / freq.QuadPart;
}
#endif