My new program:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
//int clock_gettime(clockid_t clk_id, struct timespect *tp);
#define BILLION 1000000000L;
void testClockGetTime(clockid_t clk_id , struct timespec *start , struct timespec *stop){
long temp = 0,i;
unsigned long accumsec,accumns;
if( clock_gettime( clk_id, start) == -1 ) {
perror( "clock gettime" );
exit( EXIT_FAILURE );
}
for ( i = 0; i< 24222000; i++)
temp+=temp;
if( clock_gettime( clk_id, stop) == -1 ) {
perror( "clock gettime" );
exit( EXIT_FAILURE );
}
accumsec = stop->tv_sec - start->tv_sec;
accumns = stop->tv_nsec - start->tv_nsec;
if(stop->tv_nsec<start->tv_nsec){
accumsec = accumsec - 1;
accumns = start->tv_nsec - stop->tv_nsec;
}
printf( " sec %ld\n", accumsec );
printf(" ns %ld\n", accumns );
}
int main( int argc, char **argv )
{
struct timespec start, stop;
struct timeval tds,tdse;
memset(&tds,0,sizeof(struct timeval));
memset(&tdse,0,sizeof(struct timeval));
unsigned long accumsec,accumns;
long timesec, timeusec;
printf("checking on various timers gives by clockGetTime \n");
printf("cpu time\n");
memset(&stop,0,sizeof(struct timespec));
memset(&start,0,sizeof(struct timespec));
testClockGetTime(CLOCK_PROCESS_CPUTIME_ID , &start,&stop);
memset(&start,0,sizeof(struct timespec));
memset(&stop,0,sizeof(struct timespec));
printf("real time\n");
testClockGetTime(CLOCK_REALTIME,&start,&stop);
memset(&start,0,sizeof(struct timespec));
memset(&stop,0,sizeof(struct timespec));
printf("monotonic\n");
testClockGetTime(CLOCK_MONOTONIC,&start,&stop);
memset(&start,0,sizeof(struct timespec));
memset(&stop,0,sizeof(struct timespec));
printf("thread\n");
testClockGetTime(CLOCK_THREAD_CPUTIME_ID,&start,&stop);
memset(&start,0,sizeof(struct timespec));
memset(&stop,0,sizeof(struct timespec));
gettimeofday(&tds, NULL);
long temp,i;
for ( i = 0; i< 24222000; i++)
temp+=temp;
gettimeofday(&tdse, NULL);
if( clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &stop) == -1 ) {
perror( "clock gettime" );
exit( EXIT_FAILURE );
}
// accumsec = stop.tv_sec - start.tv_sec;
// accumns = stop.tv_nsec - start.tv_nsec;
// if(stop.tv_nsec<start.tv_nsec){
// accumsec = accumsec - 1;
// accumns = start.tv_nsec - stop.tv_nsec;
// }
// printf( "proc sec %ld\n", accumsec );
// printf( "proc ns %ld\n", accumns );
// printf("before day =%ld,%ld\n",tds.tv_sec,tds.tv_usec);
// printf("after day=%ld,%ld\n",tdse.tv_sec, tdse.tv_usec);
timesec = tdse.tv_sec - tds.tv_sec;
timeusec= tdse.tv_usec- tds.tv_usec;
if(tdse.tv_usec < tds.tv_usec){
timesec = timesec - 1;
timeusec= tds.tv_usec - tdse.tv_usec;
}
printf("daytime sec =%ld\n",timesec);
printf("daytime usec=%ld\n",timeusec);
return( EXIT_SUCCESS );
}
结果:
第一次运行 > gcc getclk.c -o dfkj -lrt
getclk.c:在函数'main'中:
getclk.c:40:警告:内置函数“memset”的隐式声明不兼容
./dfkj
检查由clockGetTime提供的各种计时器
处理器时间
秒 0
ns 54502537
即时的
秒 0
ns 53748970
单调的
秒 0
ns 55456758
线
秒 0
ns 58649229
白天秒 =0
白天 usec=56991
第二次运行>./dfkj
检查由clockGetTime提供的各种计时器
处理器时间
秒 0
ns 54220021
即时的
秒 0
ns 52774966
单调的
秒 0
ns 53636163
线
秒 0
ns 53357492
白天秒 =0
白天使用 = 56176