I am trying to measure short time intervals within a few milliseconds. This turns out to be some non-trivial task, since the ubiquitous function time()works with precision of whole seconds. After some research, I developed four methods discussed in the code example below:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{
clock_t t0, t1;
double dt0;
struct timespec t2, t3;
double dt1;
struct timespec t4, t5;
double dt2;
struct timeval t6, t7;
double dt3;
volatile long long i;
t2.tv_sec = 0;
t2.tv_nsec = 0;
clock_settime(CLOCK_MONOTONIC, &t2);
clock_settime(CLOCK_REALTIME, &t2);
t0 = clock();
clock_gettime(CLOCK_REALTIME, &t2);
clock_gettime(CLOCK_MONOTONIC, &t4);
gettimeofday(&t6, NULL);
getchar();
for (i=0; i<1e9; i++) {};
gettimeofday(&t7, NULL);
clock_gettime(CLOCK_MONOTONIC, &t5);
clock_gettime(CLOCK_REALTIME, &t3);
t1 = clock();
dt0 = (double) (t1 - t0) / CLOCKS_PER_SEC;
dt1 = (double) (t3.tv_nsec - t2.tv_nsec) / 1e9;
dt2 = (double) (t5.tv_nsec - t4.tv_nsec) / 1e9;
dt3 = (double) (t7.tv_usec - t6.tv_usec) / 1e6;
printf("1. clock(): [0]=%10.0f, [1]=%10.0f, [1-0]=%10.6f sec\n", (double) t0, (double) t1, dt0);
printf("2. clock_gettime(R): [2]=%10ld, [3]=%10ld, [3-2]=%10f sec\n", (long) t2.tv_nsec, (long) t3.tv_nsec, dt1);
printf("3. clock_gettime(M): [2]=%10ld, [3]=%10ld, [3-2]=%10f sec\n", (long) t4.tv_nsec, (long) t5.tv_nsec, dt2);
printf("4. gettimeofday(): [4]=%10ld, [5]=%10ld, [5-4]=%10f sec\n", (long) t6.tv_usec, (long) t7.tv_usec, dt3);
return 0;
}
Then I compiled and ran it with:
gcc -lrt -o timer.e timer.c; time ./timer.e
I used the call getchar()to enter a couple of seconds of delay to see the difference between the time realand and reports user, and the result:
1. clock(): [0]= 0, [1]= 3280000, [1-0]= 3.280000 sec
2. clock_gettime(R): [2]= 823922476, [3]= 478650549, [3-2]= -0.345272 sec
3. clock_gettime(M): [2]= 671137949, [3]= 325864897, [3-2]= -0.345273 sec
4. gettimeofday(): [4]= 823924, [5]= 478648, [5-4]= -0.345276 sec
real 0m6.659s
user 0m3.280s
sys 0m0.010s
As you can see, the only significant result is the correlation between method 1 and the time userreported by the command time.
:
1. 2-4 ?
2. , , real number time?
- Ubuntu 10.04 LTS 64- VMware Windows 7 HP AMD.