📄 hvdebtim.cpp
字号:
if ( freq2 - (freq3 * 10) >= ROUND_THRESHOLD ) freq3++; raw_freq = total_cycles / total_ticks; norm_freq = raw_freq; freq = raw_freq * 10; if( (freq3 - freq) >= ROUND_THRESHOLD ) norm_freq++; ex_ticks = total_ticks; in_cycles = total_cycles; return raw_freq*1000000;}/********************************/void MeasureFrequency(){ nFrequencyValid = FREQUENCY_VALID;#if TIMERTYPE==WIN31_TIME liFrequency.QuadPart = 1000;//Ticks per second#elif TIMERTYPE==WIN95_TIME LARGE_INTEGER liPerfFreq; if(QueryPerformanceFrequency(&liPerfFreq)) { liFrequency = liPerfFreq.QuadPart; } else { nFrequencyValid = FREQUENCY_NOTVALID; }#elif TIMERTYPE==PENT_TIME liFrequency.QuadPart = GetRDTSCCpuSpeed(); if(liFrequency.QuadPart==0) { nFrequencyValid = FREQUENCY_NOTVALID; }#endif}}/********************************//* this is for cpp */ CVvDebugTimer::CVvDebugTimer() { ResetTime(); qwAverageAccuCount = 0; nSkipToNextOutput = 0; nDeltaHistoryPtr = 0; if(nFrequencyValid==FREQUENCY_NOTMEASURED) { MeasureFrequency(); } } void CVvDebugTimer::ResetTime() { qwAccuCount = 0; qwDeltaCount = 0; } /*************************/ void CVvDebugTimer::StartTime() {#if TIMERTYPE==WIN31_TIME dwStartCount = GetTickCount();#elif TIMERTYPE==WIN95_TIME QueryPerformanceCounter(&liStartCount);#elif TIMERTYPE==PENT_TIME VvGetAbsCpuClocks(&(liStartCount.QuadPart));#endif } /*************************/ void CVvDebugTimer::StopAndAccuTime() {#if TIMERTYPE==WIN31_TIME dwStopCount = GetTickCount(); qwDeltaCount = (__int64) dwStopCount - (__int64) dwStartCount;#else #if TIMERTYPE==WIN95_TIME QueryPerformanceCounter(&liStopCount); #elif TIMERTYPE==PENT_TIME VvGetAbsCpuClocks(&(liStopCount.QuadPart)); #endif qwDeltaCount = ((__int64)liStopCount.QuadPart -(__int64)liStartCount.QuadPart); #if TIMERTYPE==PENT_TIME /* magig number, assume that function calls etc. cost 160 cycles */ qwDeltaCount -= 160; #endif#endif qwDeltaHistory[nDeltaHistoryPtr] = qwDeltaCount; nDeltaHistoryPtr++; if(nDeltaHistoryPtr >= NBDELTASHISTORY) nDeltaHistoryPtr = 0; qwAccuCount += qwDeltaCount; } /*************************/ void CVvDebugTimer::OutputDeltaTime(char *pcLabel, S32 nMaxSkip) { char string[80]; /* Flawfinder: ignore */ nSkipToNextOutput++;//only every nMaxSkip time if(nSkipToNextOutput >= nMaxSkip) { nSkipToNextOutput = 0;#if TIMERTYPE==WIN31_TIME SafeSprintf(string, 80,"W31%s: %7.2lf ms\n", pcLabel, (double) qwDeltaCount);#else if(nFrequencyValid!=FREQUENCY_VALID) { SafeSprintf(string, 80,"%s: %7.2lf ms\n", pcLabel, 1000.*(double)qwDeltaCount/(double) liFrequency.QuadPart); } else { SafeSprintf(string, 80,"%s: Frequency not valid\n", pcLabel); }#endif OutputDebugString(string); } } /*************************/ int CVvDebugTimer::GetAccuTime(double *ptime) { int result;#if TIMERTYPE==WIN31_TIME *time = (double)qwAccuCount; result = 1#else if(nFrequencyValid == FREQUENCY_VALID) { *ptime = 1000.*(double)qwAccuCount/((double) liFrequency.QuadPart); result = 2; } else { *ptime = 0; result = 0; }#endif return(result); } /*************************/ void CVvDebugTimer::OutputAccuTime(char *pcLabel, S32 nMaxSkip) { // we run through it only every nMaxSkip times char string[80]; /* Flawfinder: ignore */ nSkipToNextOutput++;//only every nMaxSkip time if(nSkipToNextOutput >= nMaxSkip) { nSkipToNextOutput = 0; double time; int result; result = GetAccuTime(&time); if(result==1) { SafeSprintf(string, 80, "W31%s: %7.2lf ms\n", pcLabel, time); } else if(result==2) { SafeSprintf(string, 80, "%s: %7.2lf ms\n", pcLabel, time); } else { SafeSprintf(string, 80,"%s: QueryPerformanceFrequency not supported\n", pcLabel); } OutputDebugString(string); } } /*************************/ void CVvDebugTimer::LogAccuTime(char *pcLabel, S32 nMaxSkip) { // we run through it only every nMaxSkip times char string[80]; /* Flawfinder: ignore */ nSkipToNextOutput++;//only every nMaxSkip time if(nSkipToNextOutput >= nMaxSkip) { nSkipToNextOutput = 0;#if TIMERTYPE==WIN31_TIME SafeSprintf(string, 80, "W31%s: %7.2lf ms\n", pcLabel, (double)qwAccuCount);#else if(nFrequencyValid == FREQUENCY_VALID) { SafeSprintf(string, 80,"%s: %7.2lf ms\n", pcLabel, 1000.*(double)qwAccuCount/((double) liFrequency.QuadPart)); } else { SafeSprintf(string, 80,"%s: QueryPerformanceFrequency not supported\n", pcLabel); }#endif dprintf(string); } }#define VVDEBUGOUT(pcOut, string, nbMaxChar) \ if(pcOut) { (strncpy(pcOut, string, nbMaxChar)); } else {(OutputDebugString(string));} /* Flawfinder: ignore */ /*************************/ void CVvDebugTimer::AverageAndOutputAccuTime(char *pcLabel, S32 nMaxSkip, char *pcOut=0, S32 nbMaxChar=0) { // we run through it only every nMaxSkip times char string[800]; /* Flawfinder: ignore */ qwAverageAccuCount += qwAccuCount; nSkipToNextOutput++;//only every nMaxSkip time if(nSkipToNextOutput >= nMaxSkip) {#if TIMERTYPE==WIN31_TIME SafeSprintf(string, 800, "W31%s: %7.2lf ms\n", pcLabel, (double)qwAverageAccuCount/(double) nSkipToNextOutput);#else if(nFrequencyValid == FREQUENCY_VALID) { S32 ii; char appendix[20]; /* Flawfinder: ignore */ sprintf(string, "("); /* Flawfinder: ignore */ for(ii=0; ii<nDeltaHistoryPtr; ii++) { SafeSprintf(appendix, 20,"%3.1lf ", 1000.*(double)qwDeltaHistory[ii]/ ((double) liFrequency.QuadPart)); SafeStrCat(string, appendix, 800); } SafeStrCat(string, ")\n", 800); VVDEBUGOUT(pcOut, string, nbMaxChar); SafeSprintf(string, 800,"%s: %7.2lf ms\n", pcLabel, 1000.*(double)qwAverageAccuCount /((double) liFrequency.LowPart + 4294967296.*liFrequency.HighPart) /(double) nSkipToNextOutput); VVDEBUGOUT(pcOut, string, nbMaxChar); } else { SafeSprintf(string, 800,"%s: QueryPerformanceFrequency not supported\n", pcLabel); VVDEBUGOUT(pcOut, string, nbMaxChar); }#endif qwAverageAccuCount = 0; nSkipToNextOutput = 0; nDeltaHistoryPtr = 0; } }/********************************//* this is for c interface */extern "C" { // external member functions void ResetTime(CVvDebugTimer *p) { p->qwAccuCount = 0; p->qwDeltaCount = 0; } // ctors, dtors, etc. CVvDebugTimer * newCVvDebugTimer () { CVvDebugTimer *Tim; Tim = new CVvDebugTimer; Tim->ResetTime(); Tim->qwAverageAccuCount = 0; Tim->nSkipToNextOutput = 0; if(nFrequencyValid==FREQUENCY_NOTMEASURED) { MeasureFrequency(); } return Tim; } void AverageAndOutputAccuTime(CVvDebugTimer *p, char *pcLabel, S32 nMaxSkip) { if(p) p->AverageAndOutputAccuTime(pcLabel, nMaxSkip, 0, 0); } void AverageAndOutputAccuTimeString(CVvDebugTimer *p, char *pcLabel, S32 nMaxSkip, char *pcOut=0, S32 nbMaxChar=0) { if(p) p->AverageAndOutputAccuTime(pcLabel, nMaxSkip, pcOut, nbMaxChar); } int GetAccuTime(CVvDebugTimer *p, double *ptime) { int result=0; *ptime = 0.; if(p) result = p->GetAccuTime(ptime); return(result); } void OutputAccuTime(CVvDebugTimer *p, char *pcLabel, S32 nMaxSkip) { if(p) p->OutputAccuTime(pcLabel, nMaxSkip); } void LogAccuTime(CVvDebugTimer *p, char *pcLabel, S32 nMaxSkip) { if(p) p->LogAccuTime(pcLabel, nMaxSkip); } void OutputDeltaTime(CVvDebugTimer *p, char *pcLabel, S32 nMaxSkip) { if(p) p->OutputDeltaTime(pcLabel, nMaxSkip); } void StopAndAccuTime(CVvDebugTimer *p) { if(p) p->StopAndAccuTime(); } void StartTime(CVvDebugTimer *p) { if(p) p->StartTime(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -