c - 为什么 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 的纳秒。
解决方案
原因是,您(隐式)使用不同的系统时钟。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()
。
推荐阅读
- python - 如何使 wget python 模块不下载重复项?
- c# - C# 检查用户输入的特定文件名并打开文件
- java - .CSV 以逗号分隔
- python - 安装 scikit-learn Docker 镜像问题
- r - 永久设置 R 3.5.1 工作目录
- c# - c# 在类中搜索随机属性值
- c++ - 未定义对符号“_ZN3ros10NodeHandle9subscribeERNS_16SubscribeOptionsE”的引用
- reactjs - 道具改变时map()导致的意外渲染
- laravel-5 - 如何在我的 Laravel Nova 字段中保存其他数据?
- php - 如何通过使用 excel 、 csv 在图像上写入值