To solve this problem, I worked out a sleep.h, which wraps the routines and works in the POSIX way. If _NEED_SLEEP_ONLY is not defined, msleep (accepts milliseconds) and usleep (accepts microseconds) will be automatically defined too. Of course, usleep on Win32 will not have the accuracy of *NIX platforms.
Some very short explanation. I use a new type pctimer_t to express short periods, which is a double expressing time in seconds. A short program will show the use and overhead of the pctimer call:
#include <stdio.h>This program can measure periods shorter than one millisecond, which clock() (I used to use it to measure time) never achieves. The call itself takes about 1.7 microseconds on my 550-MHz Celeron ThinkPad (with MSVC; GCC takes longer). Oh, yes, maybe you will also have interest to know that the QueryPerformanceCounter call (and thus pctimer) takes less overhead on Windows 2000 than on Windows 98, or at least my tests show so.
#include <stdlib.h>
#include "pctimer.h"int main(int argc, char *argv[])
{
int i, count;
pctimer_t t1, t2;
if (argc == 2)
count = atoi(argv[1]);
else
count = 1000000;
t1 = t2 = pctimer();
for (i = 0; i < count; i++)
t2 = pctimer();
printf("%g\n", t2 - t1);
return 0;
}
All code here is considered in the public domain though I do wish my name could be retained if anyone uses them. :-)
2002-2-26, Written by Wu Yongwei