Mailing List Archive


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

Re: [tlug] timing for geeks II.



Hello :)

Having cleared the issue with printf() :) and ftime() vs. gettimeofday(),
I would like to make a step further and address the problem of
gettimeofday() vs. clock_gettime() and, perhaps separately, clock_getres(). 

Of course, Ian Wells was writing about this all the time, unfortunatly,
I was bussy with printf() (sorry). : 

* Ian Wells (ijw@example.com) [051203 14:50]:
>Reading the man pages, the CLOCK_MONOTONIC clock seems best suited to
>what you're doing.  The other one can, in theory, get changed during
>your process's execution.  Ditto gettimeofday.  This would be bad if
>you're running NTP...


* Ian Wells (ijw@example.com) [051204 12:28]:
> The 'right', if less portable, answer, is the CLOCK_MONOTONIC clock, as ntp
> (for example) will screw up gettimeofday.  gettimeofday can go backwards if
> something tampers at the wrong moment.

So to test CLOCK_MONOTONIC vs. CLOCK_REALTIME with clock_getres(), I
used this code (called rolex later on):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main(int argc, char *argv[])
{
struct timespec ts;

clock_getres(CLOCK_MONOTONIC, &ts);
printf("Clock resolution with CLOCK_MONOTONIC: %d.%09ld\n",
(int)ts.tv_sec, (long int)ts.tv_nsec);

clock_getres(CLOCK_REALTIME, &ts);
printf("Clock resolution with CLOCK_REALTIME:  %d.%09ld\n",
(int)ts.tv_sec, (long int)ts.tv_nsec);

return EXIT_SUCCESS;
}

Compiled with:
gcc -Wall -o rolex rolex.c -lrt

The output on gentoo (Gentoo Base System version 1.6.13):
$ ./rolex
Clock resolution with CLOCK_MONOTONIC: 715918496.2147481924
Clock resolution with CLOCK_REALTIME:  0.010000000
$ gcc --version
gcc (GCC) 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8)

The output on i386 debian (stable):
$ ./rolex
Clock resolution with CLOCK_MONOTONIC: 1073833280.-1073744028
Clock resolution with CLOCK_REALTIME:  0.010000000
$ gcc --version
gcc (GCC) 3.3.5 (Debian 1:3.3.5-13)

BUT:
The output on amd64 debian (testing):
$ ./rolex
Clock resolution with CLOCK_MONOTONIC: 0.000999848
Clock resolution with CLOCK_REALTIME:  0.000999848
$ gcc --version
gcc (GCC) 4.0.2 (Debian 4.0.2-2)

I am going to make more tests in a few days and post it here. 
I plan to use clock_gettime() to write time for a moment and than
compare the results for CLOCK_MONOTONIC and CLOCK_REALTIME. 

Best regards
Michal :)


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links