delorie.com/djgpp/doc/libc/libc_834.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

uclock

Syntax

 
#include <time.h>

uclock_t uclock(void);

Description

This function returns the number of uclock ticks since an arbitrary time, actually, since the first call to uclock, which itself returns zero. The number of tics per second is UCLOCKS_PER_SEC (declared in the `time.h' header file.

uclock is provided for very high-resulution timing. It is currently accurate to better than 1 microsecond (actually about 840 nanoseconds). You cannot time across two midnights with this implementation, giving a maximum useful period of 48 hours and an effective limit of 24 hours. Casting to a 32-bit integer limits its usefulness to about an hour before 32 bits will wrap.

Note that printf will only print a value of type uclock_t correctly if you use the format specifier for long long data, %lld, because uclock_t is a 64-bit integer. See section printf.

Also note that uclock reprograms the interval timer in your PC to act as a rate generator rather than a square wave generator. I've had no problems running in this mode all the time, but if you notice strange things happening with the clock (losing time) after using uclock, check to see if this is the cause of the problem.

Windows 3.X doesn't allow to reprogram the timer, so the values returned by uclock there are incorrect. DOS and Windows 9X don't have this problem.

Windows NT, 2000 and XP attempt to use the rdtsc feature of newer CPUs instead of the interval timer, because the timer tick and interval timer are not coordinated. During calibration the SIGILL signal handler is replaced to protect against systems which do not support or allow rdtsc. If rdtsc is available, uclock will keep the upper bits of the returned value consistent with the bios tick counter by re-calibration if needed. If rdtsc is not available, these systems fall back to interval timer usage, which may show an absolute error of 65536 uclock ticks in the values and not be monotonically increasing.

Return Value

The number of tics.

Portability

ANSI/ISO C No
POSIX No

Example

 
printf("%lld ticks have elapsed\n", (long long)(uclock()));
printf("%f second have elapsed\n",
       ((double)uclock()/UCLOCKS_PER_SEC));


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright © 2004     Updated Apr 2004