📄 benchmark.cpp
字号:
// Benchmark.cpp: implementation of the Benchmark class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "../performance.h"
#include "Benchmark.h"
#include <mmsystem.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// BenchMark Section
#define HIGHT_RESOLUTION_COUNTER // If define this macro, using high-resolution performance counter to get more accurate time.
// Else using function timeGetTime retrieves the system time, in milliseconds.
#define BENCHMARK_MAX_COUNT 20
#ifdef HIGHT_RESOLUTION_COUNTER
double gBenchmark_Start[BENCHMARK_MAX_COUNT];
double gBenchmark_End[BENCHMARK_MAX_COUNT];
double gBenchmark_Counter[BENCHMARK_MAX_COUNT];
double dfFreq = 1; //CPU clock frequency, initialize 1 insure the divide 0 overflow error
#else
DWORD gBenchmark_Start[BENCHMARK_MAX_COUNT];
DWORD gBenchmark_End[BENCHMARK_MAX_COUNT];
DWORD gBenchmark_Counter[BENCHMARK_MAX_COUNT];
#endif
/*------------------------------------------------------------------------------
Performance Testing
------------------------------------------------------------------------------*/
void ResetBenchmarkCounter(unsigned int idx)
{
gBenchmark_Counter[idx] = 0;
}
void ResetBenchmarkCounters(void)
{
int i;
for(i = 0; i < BENCHMARK_MAX_COUNT ; i++)
{
ResetBenchmarkCounter(i);
}
}
#ifdef HIGHT_RESOLUTION_COUNTER
void GetClockFrequent()
{
LARGE_INTEGER litmp;
QueryPerformanceFrequency(&litmp);
dfFreq = (double)litmp.QuadPart;
}
#endif
void InitBenchmark()
{
ResetBenchmarkCounters();
#ifdef HIGHT_RESOLUTION_COUNTER
GetClockFrequent();
#endif
}
/***********************************************
Function : trigger Timer
***********************************************/
void BMTimerStart(int cmdNum)
{
#ifdef HIGHT_RESOLUTION_COUNTER
LARGE_INTEGER litmp;
QueryPerformanceCounter(&litmp);
gBenchmark_Start[cmdNum] = litmp.QuadPart;
#else
gBenchmark_Start[cmdNum] = timeGetTime();
#endif
}
/***********************************************
Function : Calculate time
***********************************************/
void BMTimerEnd(int cmdNum)
{
#ifdef HIGHT_RESOLUTION_COUNTER
LARGE_INTEGER litmp;
QueryPerformanceCounter(&litmp);
gBenchmark_End[cmdNum] = litmp.QuadPart;
gBenchmark_Counter[cmdNum] += (((gBenchmark_End[cmdNum] - gBenchmark_Start[cmdNum]) / dfFreq) * 1000000);
#else
gBenchmark_End[cmdNum] = timeGetTime();
gBenchmark_Counter[cmdNum] += (gBenchmark_End[cmdNum] - gBenchmark_Start[cmdNum]);
#endif
}
void WriteData(CString strPath, CString strModelName){
// 1 Open the file and move pointer to the end of the file
CStdioFile pFile;
pFile.Open(strPath,CFile::modeWrite | CFile::typeText | CFile::modeCreate | CFile::modeNoTruncate);
pFile.SeekToEnd();
// 2 Write current time and the name of the current model
CString s;
s = "\n\n=====================================================================\nModle --- ";
s += strModelName;
s += CTime::GetCurrentTime().Format("\n%m-%d-%Y %H:%M:%S\n");
CString temp;
// 3 Write the digits, each in a row
for(int i = 0 ; i < BENCHMARK_MAX_COUNT; i ++)
{
temp.Format("Total %d",i);
// * calculate the number of '\t'
int len = temp.GetLength() / 8;
for( ; len < 2; len ++){
temp += "\t";
}
s +=temp;
#ifdef HIGHT_RESOLUTION_COUNTER
temp.Format(": %d us\n",(int)gBenchmark_Counter[i]);
#else
temp.Format(": %d ms\n",gBenchmark_Counter[i]);
#endif
s += temp;
}
//write entire string at one time
pFile.WriteString(s);
pFile.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -