Jump to content
Sign in to follow this  
  • entries
    72
  • comments
    38
  • views
    22379

QPC Timer Class

Sign in to follow this  
mozie

352 views

I had a wild hair this morning, and decided to write a quick timer class. Works off of QueryPerformanceCounter/Timer functions. This was basiclly a test to learn how to use the two functions. Seems to be working ok.

You can use, comment, criticize, or flame my design at will.

class MTimer
{
public:
__int64 Resolution;
__int64 StartedT;
__int64 SampledT;
__int64 LastT;

MTimer()
{
StartedT = 0;
SampledT = 0;
LastT = 0;
}
~MTimer()
{
}
bool Init()
{
LARGE_INTEGER freq;
if(QueryPerformanceFrequency((LARGE_INTEGER*)&freq))
{
Resolution = freq.QuadPart;
return true;
}
return false;
}
bool Start()
{
if(!Resolution) // first check for Init() success
return false;

LARGE_INTEGER samp;
if(QueryPerformanceCounter((LARGE_INTEGER*)&samp))
{
StartedT = samp.QuadPart;
LastT = StartedT;
return true;
}
return false;
}
double Sample()
{
if(!Resolution)
return -1.0;

LARGE_INTEGER samp;
if(QueryPerformanceCounter((LARGE_INTEGER*)&samp))
{
SampledT = samp.QuadPart;
double t = (double)(SampledT - LastT) / (double)Resolution;
LastT = SampledT;
return t;
}
return 0.0;
}
double Elapsed()
{
if(!Resolution)
return -1.0;

LARGE_INTEGER samp;
if(QueryPerformanceCounter((LARGE_INTEGER*)&samp))
{
SampledT = samp.QuadPart;
double t = (double)(SampledT - StartedT) / (double)Resolution;
return t;
}
return 0.0;
}
};


this is kinda what my test app looks like, just wanted to keep it a short example of use.

int main()
{
double Timed, Elapsed;

MTimer MozTime;
MozTimer.Init();
MozTimer.Start();

for(int i=0;i<100;++i)
{
Sleep(100 - i);
Timed = MozTimer.Sample();
// output or evaluate, whatever you need
}
Elapsed = MozTimer.Elapsed();
}

Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!