View Single Post
Re: need your advice on clock()
Old
  (#8)
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 Re: need your advice on clock() - 13-01-2004

Nevermind, found it - in limits.h

Here's the function now.
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;
   static double time_in_seconds;
   static double rollover = ((double) LONG_MAX + 1) / CLOCKS_PER_SEC; // fixed, won't move
   
   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 since last rollover
   time_in_seconds = (double) current_clock / CLOCKS_PER_SEC; // convert clock to seconds
 
   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 * rollover_count);
}



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