Skip to content

Instantly share code, notes, and snippets.

@travisperson
Created October 4, 2012 21:06
Show Gist options
  • Save travisperson/3836464 to your computer and use it in GitHub Desktop.
Save travisperson/3836464 to your computer and use it in GitHub Desktop.
High Frequency Timer for windows
// High Frequency Timer
//
// This is a simple timer for windows.
//
// QueryPerformanceCounter
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx
//
// QueryPerformanceFrequency
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx
//
//
// Example
// Timer t;
//
// t.start()
//
// for ( unsigned int i = 0; i < 1000; i++ )
// {
// std::cout << "Elapsed since start: " << t.getSeconds() << std::endl;
// }
#ifndef INLINE_TIMER_H
#define INLINE_TIMER_H
include <windows.h>
class Timer
{
public:
inline void start()
{
QueryPerformanceCounter(&start);
};
inline float getSeconds() const
{
LARGE_INTEGER now;
LARGE_INTEGER freq;
QueryPerformanceCounter(&now);
QueryPerformanceFrequency(&freq);
return (now.QuadPart - start.QuadPart) / static_cast<float>(freq.QuadPart);
};
private:
LARGE_INTEGER start;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment