forked from Error323/E323AI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCScopedTimer.h
90 lines (74 loc) · 2.21 KB
/
CScopedTimer.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
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
#ifndef SCOPEDTIMER_HDR
#define SCOPEDTIMER_HDR
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include "boost/thread.hpp"
#include "headers/Defines.h"
#include "headers/HAIInterface.h"
#include "headers/HEngine.h"
#define PROFILE(x) CScopedTimer t(std::string(#x), ai->cb);
// Time interval in logic frames
#define TIME_INTERVAL 1000
static const float3 colors[] = {
float3(1.0f, 0.0f, 0.0f),
float3(0.0f, 1.0f, 0.0f),
float3(0.0f, 0.0f, 1.0f),
float3(1.0f, 1.0f, 0.0f),
float3(0.0f, 1.0f, 1.0f),
float3(1.0f, 0.0f, 1.0f),
float3(0.0f, 0.0f, 0.0f),
float3(1.0f, 1.0f, 1.0f)
};
class CScopedTimer {
public:
CScopedTimer(const std::string& s, IAICallback *_cb): cb(_cb), task(s) {
initialized = true;
if (std::find(tasks.begin(), tasks.end(), task) == tasks.end()) {
taskIDs[task] = tasks.size();
#if !defined(BUILDING_AI_FOR_SPRING_0_81_2)
cb->DebugDrawerSetGraphLineColor(taskIDs[task], colors[taskIDs[task]%8]);
cb->DebugDrawerSetGraphLineLabel(taskIDs[task], task.c_str());
#endif
tasks.push_back(task);
curTime[task] = cb->GetCurrentFrame();
prevTime[task] = 0;
}
t1 = GetEngineRuntimeMillis();
}
~CScopedTimer() {
t2 = GetEngineRuntimeMillis();
if (!initialized)
return;
#if !defined(BUILDING_AI_FOR_SPRING_0_81_2)
unsigned int curFrame = cb->GetCurrentFrame();
for (size_t i = 0; i < tasks.size(); i++) {
if (tasks[i] == task) {
cb->DebugDrawerAddGraphPoint(taskIDs[task], curFrame, (t2-t1));
prevTime[task] = t2-t1;
}
else {
cb->DebugDrawerAddGraphPoint(taskIDs[tasks[i]], curFrame, prevTime[tasks[i]]);
}
if ((curFrame - curTime[tasks[i]]) >= TIME_INTERVAL)
cb->DebugDrawerDelGraphPoints(taskIDs[tasks[i]], 1);
}
#endif
}
static unsigned int GetEngineRuntimeMillis() {
boost::xtime t;
boost::xtime_get(&t, boost::TIME_UTC);
const unsigned int milliSeconds = t.sec * 1000 + (t.nsec / 1000000);
return milliSeconds;
}
private:
IAICallback *cb;
const std::string task;
unsigned int t1, t2;
bool initialized;
static std::vector<std::string> tasks;
static std::map<std::string, int> taskIDs;
static std::map<std::string, unsigned int> curTime, prevTime;
};
#endif