MAIN.C:clock_gettime在Linux中
int sum()
{
int x = 100, y =200, z;
z =x;
z=y;
printf("value is %d", z);
return 1;
}
int main()
{
double starttime, stoptime;
int a=1, b=10, c;
starttime = GetTimeStamp(); // I am calculating time here.
printf("start time is %f", starttime);
c =a;
printf("value is %d", c);
c =b;
printf("value is %d", c);
c= a+b;
printf("value is %d", c);
printf("value is %d", c);
printf("value is %d", c);
printf("value is %d", c);
stoptime =GetTimeStamp(); // I want to know the time here.
printf("stop time is %f", stoptime);
return 0;
}
timer.c中:
#define BILLION 1000000L
typedef unsigned int uint32_t;
int GetTimeStamp()
{
struct timespec start;
//double startTime;
// if((startTime = clock_gettime(CLOCK_REALTIME, &start)) == -1)
if((clock_gettime(CLOCK_REALTIME, &start)) == -1)
{
perror("clock gettime");
}
// startTime =start.tv_sec + 0.0000001 * start.tv_nsec; // to make it milli
return ((start.tv_sec + 0.0000001 * start.tv_nsec)/(double)BILLION);
}
timer.h:
#ifndef TIMESTAMP_H_
#define TIMESTAMP_H_
int GetTimeStamp();
#endif /* TIMESTAMP_H_ */
我想創建一個自由運行的定時器,並希望獲取開始執行的時間以及完成執行的時間。所以我在TIMER.c中創建了一個定時器,我在main.c中使用了它。我在程序開始時在MAIN.c中調用GetTimeStamp(),並在時間結束時再次調用它。輸出:它顯示的啓動時間和停止時間都是相同的1594.0000000
您可能想要檢查'GetTimeStamp'函數的返回類型。 –