00001
00002
00003 #include <ARToolKitPlus/extra/Profiler.h>
00004 #include <stdio.h>
00005
00006
00007 namespace ARToolKitPlus {
00008
00009
00010 #ifdef _USE_PROFILING_
00011
00012 #pragma message(">>> Building ARToolKitPlus with PROFILING ENABLED")
00013
00014 int mulCount = 0;
00015 int divCount = 0;
00016 int sqrtCount = 0;
00017 int invSqrtCount = 0;
00018 int trigCount = 0;
00019
00020 #endif
00021
00022 void
00023 Profiler::reset()
00024 {
00025 _SINGLEMARKER_OVERALL.reset();
00026 _LABELING.reset();
00027 _DETECTMARKER2.reset();
00028 _GETMARKERINFO.reset();
00029 _GETTRANSMAT.reset();
00030 _GETINITROT.reset();
00031 _GETTRANSMAT3.reset();
00032 _GETTRANSMATSUB.reset();
00033 _MODIFYMATRIX_LOOP.reset();
00034 _MODIFYMATRIX.reset();
00035 _GETNEWMATRIX.reset();
00036 _GETROT.reset();
00037 _GETANGLE.reset();
00038 }
00039
00040
00041 const Profiler::Measurement*
00042 Profiler::getMes(MES nMes) const
00043 {
00044 switch(nMes)
00045 {
00046 case SINGLEMARKER_OVERALL:
00047 return &_SINGLEMARKER_OVERALL;
00048 case LABELING:
00049 return &_LABELING;
00050 case DETECTMARKER2:
00051 return &_DETECTMARKER2;
00052 case GETMARKERINFO:
00053 return &_GETMARKERINFO;
00054 case GETTRANSMAT:
00055 return &_GETTRANSMAT;
00056 case GETINITROT:
00057 return &_GETINITROT;
00058 case GETTRANSMAT3:
00059 return &_GETTRANSMAT3;
00060 case GETTRANSMATSUB:
00061 return &_GETTRANSMATSUB;
00062 case MODIFYMATRIX_LOOP:
00063 return &_MODIFYMATRIX_LOOP;
00064 case MODIFYMATRIX:
00065 return &_MODIFYMATRIX;
00066 case GETNEWMATRIX:
00067 return &_GETNEWMATRIX;
00068 case GETROT:
00069 return &_GETROT;
00070 case GETANGLE:
00071 return &_GETANGLE;
00072 }
00073
00074 return NULL;
00075 }
00076
00077
00078 #ifdef _ARTKP_IS_WINDOWS_
00079
00080
00081 void
00082 Profiler::Measurement::reset()
00083 {
00084 sum.QuadPart = 0;
00085 }
00086
00087
00088 void
00089 Profiler::beginSection(Measurement& nM)
00090 {
00091 QueryPerformanceCounter(&nM.secBegin);
00092 }
00093
00094
00095 void
00096 Profiler::endSection(Measurement& nM)
00097 {
00098 QueryPerformanceCounter(&nM.secEnd);
00099 nM.sum.QuadPart += nM.secEnd.QuadPart - nM.secBegin.QuadPart;
00100 }
00101
00102
00103 float
00104 Profiler::getFraction(const Measurement& nNom, const Measurement& nDenom) const
00105 {
00106 long double nom = (long double)nNom.sum.QuadPart,
00107 denom = (long double)nDenom.sum.QuadPart;
00108
00109 long double fract = nom/denom;
00110 return (float)fract;
00111 }
00112
00113
00114 float
00115 Profiler::getFraction(MES nNom, MES nDenom) const
00116 {
00117 const Measurement* nom = getMes(nNom);
00118 const Measurement* denom = getMes(nDenom);
00119
00120 if(!nom || !denom)
00121 return 0.0f;
00122
00123 return getFraction(*nom, *denom);
00124 }
00125
00126
00127 float
00128 Profiler::getTime(MES nMes) const
00129 {
00130 const Measurement* mes = getMes(nMes);
00131 LARGE_INTEGER freq;
00132
00133 if(!mes)
00134 return 0.0f;
00135
00136 QueryPerformanceFrequency(&freq);
00137
00138 long double ld_sum = (long double)mes->sum.QuadPart,
00139 ld_freq = (long double)freq.QuadPart;
00140
00141 long double dt = ld_sum/ld_freq;
00142 return (float)dt;
00143 }
00144
00145
00146 #else // _ARTKP_IS_WINDOWS_
00147
00148
00149
00150
00151 void
00152 Profiler::Measurement::reset()
00153 {
00154 }
00155
00156
00157 void
00158 Profiler::beginSection(Measurement& )
00159 {
00160 }
00161
00162
00163 void
00164 Profiler::endSection(Measurement& )
00165 {
00166 }
00167
00168
00169 float
00170 Profiler::getFraction(MES , MES ) const
00171 {
00172 return 0.0f;
00173 }
00174
00175
00176 float
00177 Profiler::getTime(MES ) const
00178 {
00179 return 0.0f;
00180 }
00181
00182
00183 #endif //_ARTKP_IS_WINDOWS_
00184
00185
00186
00187 void
00188 Profiler::writeReport(const char* nFileName, unsigned int nNumRuns) const
00189 {
00190 FILE* fp = fopen(nFileName, "w");
00191 if(!fp)
00192 return;
00193
00194 #ifdef _USE_PROFILING_
00195 # ifdef _ARTKP_IS_WINDOWS_
00196 float overall = getTime(SINGLEMARKER_OVERALL);
00197
00198 if(overall==0.0f)
00199 overall = 1.0f;
00200
00201 fprintf(fp, "PROFILER REPORT (%d runs)\n\n", nNumRuns);
00202 fprintf(fp, " SINGLEMARKER_OVERALL: %.3f msecs\n", 1000.0f*overall/nNumRuns);
00203 fprintf(fp, " LABELING: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(LABELING)/nNumRuns, 100.0f*getTime(LABELING)/overall);
00204 fprintf(fp, " DETECTMARKER2: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(DETECTMARKER2)/nNumRuns, 100.0f*getTime(DETECTMARKER2)/overall);
00205 fprintf(fp, " GETMARKERINFO: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(GETMARKERINFO)/nNumRuns, 100.0f*getTime(GETMARKERINFO)/overall);
00206 fprintf(fp, " GETTRANSMAT: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(GETTRANSMAT)/nNumRuns, 100.0f*getTime(GETTRANSMAT)/overall);
00207 fprintf(fp, " GETINITROT: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(GETINITROT)/nNumRuns, 100.0f*getTime(GETINITROT)/overall);
00208 fprintf(fp, " GETTRANSMAT3: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(GETTRANSMAT3)/nNumRuns, 100.0f*getTime(GETTRANSMAT3)/overall);
00209 fprintf(fp, " GETTRANSMATSUB: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(GETTRANSMATSUB)/nNumRuns, 100.0f*getTime(GETTRANSMATSUB)/overall);
00210 fprintf(fp, " MODIFYMATRIX: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(MODIFYMATRIX)/nNumRuns, 100.0f*getTime(MODIFYMATRIX)/overall);
00211 fprintf(fp, " MODIFYMATRIX_LOOP: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(MODIFYMATRIX_LOOP)/nNumRuns, 100.0f*getTime(MODIFYMATRIX_LOOP)/overall);
00212 fprintf(fp, " GETNEWMATRIX: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(GETNEWMATRIX)/nNumRuns, 100.0f*getTime(GETNEWMATRIX)/overall);
00213 fprintf(fp, " GETROT: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(GETROT)/nNumRuns, 100.0f*getTime(GETROT)/overall);
00214
00215 fprintf(fp, "\n GETANGLE: %.3f msecs (%.2f %%)\n", 1000.0f*getTime(GETANGLE)/nNumRuns, 100.0f*getTime(GETANGLE)/overall);
00216 # else // _ARTKP_IS_WINDOWS_
00217 fprintf(fp, "PROFILER REPORT (%d runs)\n\n", nNumRuns);
00218 fprintf(fp, " ERROR: profiling currently only supported under Windows.\n");
00219 # endif // _ARTKP_IS_WINDOWS_
00220 #else // _USE_PROFILING_
00221 fprintf(fp, "PROFILER REPORT (%d runs)\n\n", nNumRuns);
00222 fprintf(fp, " ERROR: profiling was disabled at compiletime.\n");
00223 #endif // _USE_PROFILING_
00224
00225 fclose(fp);
00226 }
00227
00228
00229 bool
00230 Profiler::isProfilingEnabled()
00231 {
00232 #ifdef _USE_PROFILING_
00233 return true;
00234 #else
00235 return false;
00236 #endif
00237 }
00238
00239
00240
00241 }