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 によって提供されます
CPU時間
秒 0
ns 54502537
リアルタイム
秒 0
ns 53748970
単調
秒 0
ns 55456758
スレッド
秒 0
ns 58649229
昼間秒=0
昼間usec=56991
2 回目の実行>./dfkj
さまざまなタイマーのチェックは、clockGetTime によって提供されます
CPU時間
秒 0
ns 54220021
リアルタイム
秒 0
ns 52774966
単調
秒 0
ns 53636163
スレッド
秒 0
ns 53357492
昼間秒=0
昼間usec=56176