Sign in to follow this  
mind_wipe

High Resolution Timers

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this