View Single Post
need your advice on clock()
Old
  (#1)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default need your advice on clock() - 13-01-2004

I'm making a facility to get rid of gpGlobals->time everywhere I can.

This function is supposed to return the time in seconds since the process started, even after the clock() value has overflown.

Since I don't want to wait 24 days to check if my function works, could someone tell me if my implementation is correct ?
Code:
float ProcessTime (void)
{
   // this function returns the time in seconds elapsed since the executable process started.
   // The rollover check ensures the program will continue running after clock() will have
   // overflown its integer value (it does so every 24 days or so). With this rollover check
   // we have a lifetime of more than billion years, w00t!
   // thanks to botmeister for the rollover check idea.
 
   static long current_clock;
   static long prev_clock = 0;
   static long rollover_count = 0;
   float time_in_seconds;
   float rollover_difference;
 
   current_clock = clock (); // get system clock
 
   // has the clock overflown ?
   if (current_clock < prev_clock)
	  rollover_count++; // omg, it has, we're running for more than 24 days!
 
   // now convert the time to seconds and calculate the rollover difference
   time_in_seconds = (float) current_clock / CLOCKS_PER_SEC; // convert clock to seconds
   rollover_difference = (float) 286331153 / CLOCKS_PER_SEC * rollover_count;
 
   prev_clock = current_clock; // keep track of current time for future calls of this function
 
   // and return the time in seconds, adding the overflow differences if necessary.
   return (time_in_seconds + rollover_difference);
}



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote