QPC Timer Class

Published March 03, 2005
Advertisement
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();}
Previous Entry uploaded
Next Entry the ogre is here
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

Not bad.

1146 views

Zoned out.

1021 views

more

1101 views

Slow down champ.

917 views

I'll be back

880 views

Busy.

1036 views

oh teh noes!!

923 views
Advertisement