Author: Dan Newman
Date: 23:21:37 09/24/99
Go up one level in this thread
On September 24, 1999 at 10:19:45, Robert Hyatt wrote:
>On September 24, 1999 at 07:50:47, GBes wrote:
>
>>you can use clock( ) for this.
>>
>>clock_t start,finish;
>>
>>start = clock();
>>// ...
>>finish = clock();
>>double difference = (double)(finish-start)/CLOCKS_PER_SECOND;
>
>
>clock() is bad. It returns processor time, and not wall-clock time. If
>something else runs with you, you will use more time than expected because
>your cpu time doesn't increase linearly with elapsed time.
>
>For unix, I use gettimeofday() which returns a very accurate wall-clock time
>accurate to microseconds if you want...
I haven't tried this in Linux, but the best I've ever gotten on a PC
(Windows/DOS) is about a 55ms resolution. I think this is due to the
fact that the interrupt that maintains the time goes off 18.2 times/s (IIRC).
Unfortunately, no Windows/DOS compiler that I've used has the gettimeofday()
function (unless GNU does--I haven't looked there). They do tend to have a
rather large number of time functions to choose from though.
What I use is this:
------------Timer.c----------------------
/*
+------------------------------+
| |
| Time measuring function. |
| |
+------------------------------+
*/
#include <sys\timeb.h>
#include <stdio.h>
#include "timer.h"
static unsigned long offset_time;
//
// Call this once to set trimmer variable, offset_time.
//
void init_time()
{
struct timeb timebuf;
ftime( &timebuf);
offset_time = timebuf.time;
}
//
// Returns wallclock time in milliseconds. Has only
// about 55 ms resolution on a PC.
//
unsigned long wallclock()
{
struct timeb timebuf;
ftime( &timebuf);
return (timebuf.time - offset_time) * 1000ul + timebuf.millitm;
}
--------------------------
The "time" member of timeb has the number of seconds since Jan 1, 1970
UTC which if converted to milliseconds would overflow the 32-bit integer,
so I call init_time() once to record this value in "offset_time" and
just subtract it in the wallclock() function.
The Windows API has GetTickCount() which will return the number of
milliseconds elapsed since Windows start-up. I haven't tried this one,
but I suspect it will suffer from the same resolution problem. This
of course runs out of ms and wraps around in about 50 days--which of
course will never be a problem...
-Dan.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.