🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

High Resolution Timers

Started by
1 comment, last by mind_wipe 13 years, 7 months ago
Okay, so I've done what I normally do to get what I though was accurate timing in Linux. It appears that the time is being reset and/or wrapping. This is the timing procedure I use:

uint 	G_GetTime( void ){	struct timespec			tv ;		clock_gettime( CLOCK_MONOTONIC, &tv ) ;		return tv.tv_nsec / 1000 ;}


I though that tv.tv_nsec records nano time so doesn't that mean you have to divide by 1000 to get milliseconds? Anyways, I've ran a little debug to output the time value and I get this:

time: 88
time: 22998
time: 125476
time: 245781
time: 388564
time: 567986
time: 805240
time: 1054080
time: 1322626
time: 1617701
etc..


I have another procedure that records the time initially, yet another procedure that returns the difference(hopefully in milliseconds) between the current time and the one saved. As you can see from above something is wrong and I have overlooked something. These are the other procedures:

//	---------------------------------------------------------------------------void	G_SetState	(	uint					state	){	st_curr					= state ;	st_time					= G_GetTime( ) ;	st_timeLaps				= 0 ;}//	---------------------------------------------------------------------------uint	G_GetStateTime( void ){	st_timeLaps				+= ( G_GetTime( ) - st_time ) ;	return st_timeLaps ;}


I'm so confused is to why it's not working... Can someone help me please?
Advertisement
Quote: Original post by mind_wipe
I though that tv.tv_nsec records nano time so doesn't that mean you have to divide by 1000 to get milliseconds?


No, you have to divide by a million.
Also, you're not using the tv_sec field, you should do this:

tv.tv_sec*1000 + tv.tv_nsec/1000000
to get milliseconds.

Your other function should just be:
return G_GetTime() - st_time;
if you want it to return the difference from now to when you called G_SetState.
Dude, you rock! I'm kinda confused about how that timer works. But oh well... thanks a bunch you save me a huge headache.

This topic is closed to new replies.

Advertisement