我确信答案很简单,但我不太明白。我正在尝试struct timespec
使用以下代码计算两者之间的增量:
struct timespec start, finish, diff;
int ndiff;
/* Structs are filled somewhere else */
diff.tv_sec = finish.tv_sec - start.tv_sec;
ndiff = finish.tv_nsec - start.tv_nsec;
if (ndiff < 0) {
diff.tv_sec--;
ndiff = 1L - ndiff;
}
diff.tv_nsec = ndiff;
printf("Elapsed time: %ld.%ld seconds.\n", diff.tv_sec, diff.tv_nsec);
但是,输出总是类似于Elapsed time: 0.300876000 seconds.
这似乎表明我正在丢失纳秒的最后三位数字(因为这些数字不应该总是为零)。有人能指出是什么原因造成的吗?