Skip to content

Instantly share code, notes, and snippets.

@baiyanhuang
Created June 17, 2012 02:16
Show Gist options
  • Save baiyanhuang/2943163 to your computer and use it in GitHub Desktop.
Save baiyanhuang/2943163 to your computer and use it in GitHub Desktop.
#pragma once
#include <Windows.h>
#include <iostream>
// How to use:
// Example 1:
// double d1 = Tick();
// double d2 = Tick();
// double time = d2 - d1;
//
// Example 2:
// {
// CAutoTimer at;
// // ...
// }
// this is a internal function
static LONGLONG GetPerformanceFrequency()
{
LARGE_INTEGER qpFrequency;
::QueryPerformanceFrequency(&qpFrequency);
return qpFrequency.QuadPart;
}
double Tick()
{
static LONGLONG llFrequency = GetPerformanceFrequency();
LARGE_INTEGER qpCounter;
::QueryPerformanceCounter(&qpCounter);
return qpCounter.QuadPart / (double)llFrequency;
}
class CAutoTimer
{
public:
CAutoTimer()
{
start_ = Tick();
}
~CAutoTimer()
{
end_ = Tick();
Output();
}
inline double GetElapsedTime() const {return end_ - start_;}
inline void Output()
{
char output[100];
sprintf(output, "Elapsed Time: %f\n", GetElapsedTime());
std::cout << output << std::endl;
}
private:
double start_;
double end_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment