Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ARToolKitPlus/src/extra/Profiler.cpp
blob: 83ebc95da60670a3ea01a443833efc5bece7d510 (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241


#include <ARToolKitPlus/extra/Profiler.h>
#include <stdio.h>


namespace ARToolKitPlus {


#ifdef _USE_PROFILING_

#pragma message(">>> Building ARToolKitPlus with PROFILING ENABLED")

int mulCount = 0;
int divCount = 0;
int sqrtCount = 0;
int invSqrtCount = 0;
int trigCount = 0;

#endif

void
Profiler::reset()
{
	_SINGLEMARKER_OVERALL.reset();
	_LABELING.reset();
	_DETECTMARKER2.reset();
	_GETMARKERINFO.reset();
	_GETTRANSMAT.reset();
	_GETINITROT.reset();
	_GETTRANSMAT3.reset();
	_GETTRANSMATSUB.reset();
	_MODIFYMATRIX_LOOP.reset();
	_MODIFYMATRIX.reset();
	_GETNEWMATRIX.reset();
	_GETROT.reset();
	_GETANGLE.reset();
}


const Profiler::Measurement*
Profiler::getMes(MES nMes) const
{
	switch(nMes)
	{
	case SINGLEMARKER_OVERALL:
		return &_SINGLEMARKER_OVERALL;
	case LABELING:
		return &_LABELING;
	case DETECTMARKER2:
		return &_DETECTMARKER2;
	case GETMARKERINFO:
		return &_GETMARKERINFO;
	case GETTRANSMAT:
		return &_GETTRANSMAT;
	case GETINITROT:
		return &_GETINITROT;
	case GETTRANSMAT3:
		return &_GETTRANSMAT3;
	case GETTRANSMATSUB:
		return &_GETTRANSMATSUB;
	case MODIFYMATRIX_LOOP:
		return &_MODIFYMATRIX_LOOP;
	case MODIFYMATRIX:
		return &_MODIFYMATRIX;
	case GETNEWMATRIX:
		return &_GETNEWMATRIX;
	case GETROT:
		return &_GETROT;
	case GETANGLE:
		return &_GETANGLE;
	}

	return NULL;
}


#ifdef _ARTKP_IS_WINDOWS_


void
Profiler::Measurement::reset()
{
	sum.QuadPart = 0;
}


void
Profiler::beginSection(Measurement& nM)
{
	QueryPerformanceCounter(&nM.secBegin);
}


void
Profiler::endSection(Measurement& nM)
{
	QueryPerformanceCounter(&nM.secEnd);
	nM.sum.QuadPart += nM.secEnd.QuadPart - nM.secBegin.QuadPart;
}


float
Profiler::getFraction(const Measurement& nNom, const Measurement& nDenom) const
{
	long double nom = (long double)nNom.sum.QuadPart,
				denom = (long double)nDenom.sum.QuadPart;

	long double fract = nom/denom;
	return (float)fract;
}


float
Profiler::getFraction(MES nNom, MES nDenom) const
{
	const Measurement* nom = getMes(nNom);
	const Measurement* denom = getMes(nDenom);

	if(!nom || !denom)
		return 0.0f;

	return getFraction(*nom, *denom);
}


float
Profiler::getTime(MES nMes) const
{
	const Measurement* mes = getMes(nMes);
	LARGE_INTEGER freq;

	if(!mes)
		return 0.0f;

	QueryPerformanceFrequency(&freq);

	long double ld_sum = (long double)mes->sum.QuadPart,
				ld_freq = (long double)freq.QuadPart;

	long double dt = ld_sum/ld_freq;
	return (float)dt;
}


#else // _ARTKP_IS_WINDOWS_


// supply only stub methods for non-windows systems
//
void
Profiler::Measurement::reset()
{
}


void
Profiler::beginSection(Measurement& /*nM*/)
{
}


void
Profiler::endSection(Measurement& /*nM*/)
{
}


float
Profiler::getFraction(MES /*nNom*/, MES /*nDenom*/) const
{
	return 0.0f;
}


float
Profiler::getTime(MES /*nMes*/) const
{
	return 0.0f;
}


#endif //_ARTKP_IS_WINDOWS_



void
Profiler::writeReport(const char* nFileName, unsigned int nNumRuns) const
{
	FILE* fp = fopen(nFileName, "w");
	if(!fp)
		return;

#ifdef _USE_PROFILING_
#  ifdef _ARTKP_IS_WINDOWS_
	float overall = getTime(SINGLEMARKER_OVERALL);

	if(overall==0.0f)			// prevent division by 0
		overall = 1.0f;			// on non-windows systems

	fprintf(fp, "PROFILER REPORT (%d runs)\n\n", nNumRuns);
	fprintf(fp, "  SINGLEMARKER_OVERALL:                    %.3f msecs\n", 1000.0f*overall/nNumRuns);
	fprintf(fp, "      LABELING:                            %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(LABELING)/nNumRuns, 100.0f*getTime(LABELING)/overall);
	fprintf(fp, "      DETECTMARKER2:                       %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(DETECTMARKER2)/nNumRuns, 100.0f*getTime(DETECTMARKER2)/overall);
	fprintf(fp, "      GETMARKERINFO:                       %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(GETMARKERINFO)/nNumRuns, 100.0f*getTime(GETMARKERINFO)/overall);
	fprintf(fp, "      GETTRANSMAT:                         %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(GETTRANSMAT)/nNumRuns, 100.0f*getTime(GETTRANSMAT)/overall);
	fprintf(fp, "          GETINITROT:                      %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(GETINITROT)/nNumRuns, 100.0f*getTime(GETINITROT)/overall);
	fprintf(fp, "          GETTRANSMAT3:                    %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(GETTRANSMAT3)/nNumRuns, 100.0f*getTime(GETTRANSMAT3)/overall);
	fprintf(fp, "              GETTRANSMATSUB:              %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(GETTRANSMATSUB)/nNumRuns, 100.0f*getTime(GETTRANSMATSUB)/overall);
	fprintf(fp, "                  MODIFYMATRIX:            %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(MODIFYMATRIX)/nNumRuns, 100.0f*getTime(MODIFYMATRIX)/overall);
	fprintf(fp, "                      MODIFYMATRIX_LOOP:   %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(MODIFYMATRIX_LOOP)/nNumRuns, 100.0f*getTime(MODIFYMATRIX_LOOP)/overall);
	fprintf(fp, "                          GETNEWMATRIX:    %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(GETNEWMATRIX)/nNumRuns, 100.0f*getTime(GETNEWMATRIX)/overall);
	fprintf(fp, "                              GETROT:      %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(GETROT)/nNumRuns, 100.0f*getTime(GETROT)/overall);

	fprintf(fp, "\n  GETANGLE:                                 %.3f msecs  (%.2f %%)\n", 1000.0f*getTime(GETANGLE)/nNumRuns, 100.0f*getTime(GETANGLE)/overall);
#  else  // _ARTKP_IS_WINDOWS_
	fprintf(fp, "PROFILER REPORT (%d runs)\n\n", nNumRuns);
	fprintf(fp, "  ERROR: profiling currently only supported under Windows.\n");
#  endif // _ARTKP_IS_WINDOWS_
#else  // _USE_PROFILING_
	fprintf(fp, "PROFILER REPORT (%d runs)\n\n", nNumRuns);
	fprintf(fp, "  ERROR: profiling was disabled at compiletime.\n");
#endif // _USE_PROFILING_

	fclose(fp);
}


bool
Profiler::isProfilingEnabled()
{
#ifdef _USE_PROFILING_
	return true;
#else
	return false;
#endif
}



}  // namespace ARToolKitPlus