首页 > 解决方案 > 为什么 time() 报告的时间有时比 C 代码中 timespec_get() 的秒组件慢 1 秒?

问题描述

以下代码片段:

struct timespec ts;
for (int x = 0; x < 100000000; x++) {
    timespec_get(&ts, TIME_UTC);
    long cTime = (long) time(NULL);
    if (cTime != ts.tv_sec && ts.tv_nsec < 3000000) {
        printf("cTime: %ld\n", cTime);
        printf("ts.tv_sec: %ld\n", ts.tv_sec);
        printf("ts.tv_nsec: %ld\n", ts.tv_nsec);
    }
}

产生这个输出:

...
cTime: 1579268059
ts.tv_sec: 1579268060
ts.tv_nsec: 2527419
cTime: 1579268059
ts.tv_sec: 1579268060
ts.tv_nsec: 2534036
cTime: 1579268059
ts.tv_sec: 1579268060
ts.tv_nsec: 2540359
cTime: 1579268059
ts.tv_sec: 1579268060
ts.tv_nsec: 2547039
...

cTime为什么和之间的差异ts.tv_sec?请注意,如果将条件更改为 ,则不会出现此问题ts.tv_nsec >= 3000000。该问题依赖于小于 3000000 的纳秒。

标签: ctimeposixtimespec

解决方案


原因是,您(隐式)使用不同的系统时钟。timespec_get()使用高分辨率全系统实时时钟,而time()使用粗略实时时钟。

尝试使用

clock_gettime(CLOCK_REALTIME_COARSE, &ts);

而不是你的timespec_get(),那么差异应该消失。

编辑:

这可以在Linux 内核源代码 vclock_gettime.c中看到

确实,在这里看到这个问题有点微妙。CLOCK_REALTIME_COARSE使用的结构成员的秒部分CLOCK_REALTIME包含相同的值,但纳秒部分不同;它CLOCK_REALTIME可以大于1000000000(即一秒)。在这种情况下,它在通话中得到修复:

ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
ts->tv_nsec = ns;

既不使用CLOCK_REALTIME_COARSE也不使用执行此校正time()CLOCK_REALTIME这解释了和之间的区别time()


推荐阅读