• Create Account

## C++ Timer Library

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1Tarviathun  Members

Posted 12 January 2005 - 09:16 PM

Currently, I'm looking for a decent timer library for a 2D RTS engine that I'm making. I've looked around the internet but haven't found anything. I could use an API specific timer, such as those in DX, OpenGL, or something lesser known such as Grim. But I'd prefer to have seperate libraries for everything, allowing the maximum amount of flexibility when it comes to porting the engine to different APIs for performance comparison. If anyone could recommend a decent timer library for my game, I'd really appreciate it. Thanks for the time.

### #2Gorax  Members

Posted 12 January 2005 - 11:59 PM

Time.h has everything you need. It's as simple as calling clock() to get the number of millis passed since the app started. For the difference in seconds, it's like this:
clock_t initial = clock();//do stuff hereclock_t current = clock();float difference = (current - initial) / 1000.0f;//1000 millis = 1 second ;)

### #3maximAL  Members

Posted 13 January 2005 - 12:54 AM

afaik the granularity of clock(0) really sucks, so its not a good choice for a decent game...
plus, it only takes the CPU time, right? what if your programs task doesn't get 100% CPU time?

@Tarviathun: i don't know whether the boost library has good timers, but i'd just write my own timer class to wrap some DX timer (eg.), so you can easily replace it if necessary.

### #4Tarviathun  Members

Posted 13 January 2005 - 08:38 AM

I'll probably do what you said, max. Wrap a timer library from DX. I would presume it has a high-resolution timer. The reason why I don't use the standard clock in Time.h is I've heard that is has some issues with accuracy.

### #5Drew_Benton  Members

Posted 13 January 2005 - 11:25 AM

Here's the timer class I made from a few various tutorials.

#include <mmsystem.h>#pragma comment(lib,"winmm.lib")// Ctime.hclass CTime{private:	float dt, td, td2;	__int64       frequency;									// Timer Frequency	float         resolution;									// Timer Resolution	unsigned long mm_timer_start;								// Multimedia Timer Start Value	unsigned long mm_timer_elapsed;								// Multimedia Timer Elapsed Time	bool		  performance_timer;							// Using The Performance Timer?	__int64       performance_timer_start;						// Performance Timer Start Value	__int64       performance_timer_elapsed;					// Performance Timer Elapsed Timepublic:	CTime();	void Update();	float Get_dt() const;	float GetTime() const;	void Initialize();};

// CTime.cppvoid CTime::Update() {	td2 = GetTime();	dt = (td2-td) * 0.1f; 	td = td2;}CTime::CTime(){	dt = 0;	td2 = 0;	td = 0;	frequency = 0;	performance_timer_start = 0;	resolution = 0;	mm_timer_start = 0;	mm_timer_elapsed = 0;	performance_timer_elapsed = 0;	performance_timer = 0;	Initialize();}void CTime::Initialize()													// Initialize Our Timer (Get It Ready){	// Check To See If A Performance Counter Is Available	// If One Is Available The Timer Frequency Will Be Updated	if (!QueryPerformanceFrequency((LARGE_INTEGER *) &frequency))	{		// No Performace Counter Available		performance_timer	= FALSE;					// Set Performance Timer To FALSE		mm_timer_start	= timeGetTime();			// Use timeGetTime() To Get Current Time		resolution		= 1.0f/1000.0f;				// Set Our Timer Resolution To .001f		frequency			= 1000;						// Set Our Timer Frequency To 1000		mm_timer_elapsed	= mm_timer_start;		// Set The Elapsed Time To The Current Time	}	else	{		// Performance Counter Is Available, Use It Instead Of The Multimedia Timer		// Get The Current Time And Store It In performance_timer_start		QueryPerformanceCounter((LARGE_INTEGER *) &performance_timer_start);		performance_timer			= TRUE;				// Set Performance Timer To TRUE		// Calculate The Timer Resolution Using The Timer Frequency		resolution				= (float) (((double)1.0f)/((double)frequency));		// Set The Elapsed Time To The Current Time		performance_timer_elapsed	= performance_timer_start;	}}float CTime::Get_dt() const { 	return dt; }float CTime::GetTime() const						// Get Time In Milliseconds{	__int64 time;									// time Will Hold A 64 Bit Integer	if (performance_timer)							// Are We Using The Performance Timer?	{		QueryPerformanceCounter((LARGE_INTEGER *) &time);	// Grab The Current Performance Time		// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)		return ( (float) ( time - performance_timer_start) * resolution)*1000.0f;	}	else	{		// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)		return( (float) ( timeGetTime() - mm_timer_start) * resolution)*1000.0f;	}}

To use it, it's very easy:
CTime timer;

timer.Initialize();

timer.Update();

whenever you want to get the elapsed time:
timer.GetTime();

whenever you want to get the frame difference time
timer.Get_dt();

It should be bug free - I've had no problems with it [smile]. I just take credit for wrapping it up [lol] - the code is not my original ideas. I believe I had used some ideas from Cone3D, NeHe, and GameTutorials sometime ago.

- Drew

 Also take a look at this page for info on the avaliable timers and why I use the one I did.[/edit]

[Edited by - Drew_Benton on January 13, 2005 10:25:15 PM]

### #6Tarviathun  Members

Posted 13 January 2005 - 04:18 PM

Thank you a whole lot. I think I'll use that for now, seeing as it's simple and effective. I'll definately give you credit for it. Thanks.

### #7Drew_Benton  Members

Posted 13 January 2005 - 04:30 PM

No problem! I would have to give credit to NeHe - I found some of the source. Good luck! [smile]

### #8Tarviathun  Members

Posted 14 January 2005 - 02:17 PM

Thanks. Will do.

### #9Catafriggm  Members

Posted 22 May 2005 - 08:53 AM

void WINAPI GetCycleCount(ULARGE_INTEGER &ulCycleCount){     __asm      { 		push ecx        rdtsc         mov ecx, ulCycleCount         mov DWORD PTR[ecx], eax         mov DWORD PTR[ecx+4], edx 		pop ecx     } }

That'll get the CPU cycle counter. Should be accurate to +/- 10 cycles or so, if you're running in realtime priority class.

### #10Wojtek  Members

Posted 05 May 2009 - 07:43 AM

void CTime::Update() {	td2 = GetTime();	dt = (td2-td) * 0.1f; 	td = td2;}

Could somebody tell me why do we multiply the variable dt in the above timer update function by 0.1? That would give us the difference time not in ms, but in 0.01s.
--------------------------------------www.younghifi.comThe crazy world of a young audiophile

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.