# High Resolution Timers

This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
Quote:
 Original post by mind_wipeI 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.

##### Share on other sites
Dude, you rock! I'm kinda confused about how that timer works. But oh well... thanks a bunch you save me a huge headache.

1. 1
2. 2
3. 3
4. 4
Rutin
11
5. 5

• 12
• 19
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632666
• Total Posts
3007722
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

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!