[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gettimeofday() and clock



Mads Bondo Dydensborg wrote:
> On Sun, 25 Aug 2002, Steve Baker wrote:
> 
>>usleep is given an argument in MICROSECONDS - but in practice it can't wake
>>your program up any faster than the kernel timeslice - which is 1/50th second.

> Eh? Depends on platform, I believe, last I checked was 100Hz on intel, 
> 1000Hz on alpha.

Really?   I could have sworn it was 50Hz on Intel - because when my program
does a short usleep (say 1 millisecond), it generally sleeps for ~20ms - which
is 1/50th second.  Hence, I deduce that the kernel only wakes up and reschedules
my program 50 times a second - and not 100Hz as you suggest.

I also believe the 50Hz figure because the *original* UNIX on PDP-11's used
50Hz - and I presumed that Linus picked the same number for compatibility.

Try running this:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>

double getTime ()
{
   struct timeval tv ;
   gettimeofday ( & tv, NULL ) ;
   return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0 ;
}

int main ()
{
   while ( 1 )
   {
     double t1, t2 ;
     t1 = getTime () ;
     usleep ( 1000 ) ; /* 1ms */
     t2 = getTime () ;
     printf ( "%d msec\n", (int)((t2 - t1) * 1000.0) ) ;
   }
}

I'd *really* like to see a shorter timeslice on Intel.  With
2GHz CPU's, even 100Hz is like an eternity.

A 1000Hz kernel timeslice - and (in consequence) a usleep that was
accurate to ~1ms instead of ~20ms - would solve a *TON* of problems
for graphics programs that want to run at 60Hz and yet still use
'usleep' to avoid blocking the CPU.

----------------------------- Steve Baker -------------------------------
Mail : <sjbaker1@airmail.net>   WorkMail: <sjbaker@link.com>
URLs : http://www.sjbaker.org
        http://plib.sf.net http://tuxaqfh.sf.net http://tuxkart.sf.net
        http://prettypoly.sf.net http://freeglut.sf.net
        http://toobular.sf.net   http://lodestone.sf.net