-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProfileClass.cpp
78 lines (67 loc) · 1.88 KB
/
ProfileClass.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
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
#include "ProfileClass.h"
CProfiler& CProfiler::GetInst()
{
static CProfiler inst;
return inst;
}
CProfiler::CProfiler()
{
m_Functions.clear();
}
CProfiler::~CProfiler(){}
void CProfiler::AddFunction(DWORD Index, const string& name)
{
Func f(Index, name);
m_Functions.insert(pair<DWORD, Func>(Index, f));
}
void CProfiler::BeginFunction(DWORD FuncIndex)
{
#ifdef _DEBUG
FuncIt it = m_Functions.find(FuncIndex);
if(it == m_Functions.end())
{
MessageBoxA(NULL, "Ungültiger Funktionsindex!", "Fehler", 0);
return;
}
#else
FuncIt it = m_Functions.find(FuncIndex);
#endif
QueryPerformanceCounter(&it->second.tmpTime);
it->second.NumCalls++;
m_LastBeganFunction = it;
m_LastFuncIndex = FuncIndex;
}
void CProfiler::EndFunction(DWORD FuncNr)
{
LARGE_INTEGER tmp;
QueryPerformanceCounter(&(tmp));
tmp.QuadPart -= m_LastBeganFunction->second.tmpTime.QuadPart;
m_LastBeganFunction->second.TimeSum.QuadPart += tmp.QuadPart;
}
bool operator<(const Func& fu1, const Func& fu2)
{
LARGE_INTEGER t1, t2, freq;
t1.QuadPart = 0;
t2.QuadPart = 0;
QueryPerformanceFrequency(&freq);
return fu1.TimeSum.QuadPart < fu2.TimeSum.QuadPart;
}
void CProfiler::GetResults(ostream& os)
{
string s;
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
vector<Func> tmp;
for(FuncIt it = m_Functions.begin(); it != m_Functions.end(); it++)
tmp.push_back(it->second);
sort(tmp.begin(), tmp.end());
os << "Funktionsaufrufe" << endl;
for(size_t f = 0; f < tmp.size(); f++)
{
double t = double(tmp[f].TimeSum.QuadPart) / freq.QuadPart;
t /= tmp[f].NumCalls;
os << tmp[f].name << endl;
os << "Anzahl der Aufrufe: " << tmp[f].NumCalls << endl;
os << "Durchschnittliche Laufzeit: " << t << " Summe: " << t * tmp[f].NumCalls << endl << endl;
}
}