在以下程序中,我嘗試測量作業的執行時間(for循環)。大多數情況下,它可以正常工作,但有時候,它會返回負值!我的第一個猜測是變量可能會溢出。任何人都可以請讓我知道我是否正確?我該如何解決這個問題?獲取負值使用clock_gettime
感謝
int main(int argc, char **argv)
{
long int ST;
long int ET;
struct timespec gettime_now;
clock_gettime(CLOCK_REALTIME, &gettime_now);
ST= gettime_now.tv_nsec;
for (i=0; i < 1000; i++)
a[i]=b[i];
clock_gettime(CLOCK_REALTIME, &gettime_now);
ET= gettime_now.tv_nsec;
printf("Time diff: %ld\n", ET-ST);
}
請參閱: http://stackoverflow.com/questions/17659867/how-can-i-check-how-many-times-a-function-was-called-with-a-3-seconds-間隔/ 如何計時功能。 –