Skip to content

Instantly share code, notes, and snippets.

@baiyanhuang
Created April 5, 2011 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baiyanhuang/902874 to your computer and use it in GitHub Desktop.
Save baiyanhuang/902874 to your computer and use it in GitHub Desktop.
Simple timing function and class to profile program in Windows
LONGLONG GetFrequency()
{
LARGE_INTEGER qpFrequency;
::QueryPerformanceFrequency(&qpFrequency);
return qpFrequency.QuadPart;
}
double Tick()
{
static LONGLONG llFrequency = GetFrequency();
LARGE_INTEGER qpCounter;
::QueryPerformanceCounter(&qpCounter);
return qpCounter.QuadPart / (double)llFrequency;
}
// Example
double c1 = Tick();
::Sleep(3000);
double c2 = Tick();
class CAutoTimer
{
public:
CAutoTimer()
{
start = Tick();
}
~CAutoTimer()
{
double end = Tick();
CString strOutput;
strOutput.Format(_T("Time: %4.2f seconds!"), end-start);
AfxMessageBox(strOutput);
}
private:
double start;
};
// Example
{
CAutoTimer auto;
::Sleep(3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment