Created
June 17, 2012 02:16
-
-
Save baiyanhuang/2943163 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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