Clock_gettime is more accurate than gettimeofday
Perform a simple test and use clock_gettime to perform the test.
Test. c
#include<time.h> #include<stdio.h>
#define MILLION 1000000
int main(void) { struct timespec tpstart; struct timespec tpend; long timedif;
clock_gettime(CLOCK_MONOTONIC, &tpstart); clock_gettime(CLOCK_MONOTONIC, &tpend); timedif = MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000; fprintf(stdout, "it took %ld microseconds/n", timedif);
return 0; }
|
Under Linux 2.6 kernel
Gcc-O test. C-LRT
./Test
Expected result:
It took 2 microseconds
Use gettimeofday to perform the test.
#include<time.h> #include<stdio.h>
#define MILLION 1000000
int main(void) { struct timespec tpstart; struct timespec tpend; long timedif;
gettimeofday(&tpstart, NULL); gettimeofday(&tpend, NULL); timedif = MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000; fprintf(stdout, "it took %ld microseconds/n", timedif);
return 0; }
|
Gcc-O test. c
./Test
Expected result:
It took 0 microseconds AIX also gets the same result
In addition, we can use the clock_gettime function to test that the 64-bit program runs faster than the 32-bit program.
On AIX p570, 16 machines with CPU 15.5g memory tested
The result obtained in 64-Bit mode is 0 microseconds.
The result of the 32-Bit mode is 1 microsecond.