【发布时间】:2016-12-06 02:44:03
【问题描述】:
我正在尝试在 1GHz MIPS 路由器上从 CLOCK_REALTIME 获得纳秒分辨率。当我为 x86 编译以下代码并在 1GHz vm 上运行时,我得到了纳秒级分辨率。当我针对 mips 进行编译并在 1GHz 路由器上运行时,它似乎可以精确到微秒。
#include <stdio.h>
#include <time.h>
int main(int argc, char **argv, char **arge) {
struct timespec tps, tpe;
if ((clock_gettime(CLOCK_REALTIME, &tps) != 0)
|| (clock_gettime(CLOCK_REALTIME, &tpe) != 0)) {
perror("clock_gettime");
return -1;
}
printf("%lu s, %lu ns\n", tpe.tv_sec-tps.tv_sec,
tpe.tv_nsec-tps.tv_nsec);
return 0;
}
这里是采样时间:
vagrant@vagrant-ubuntu-trusty-64:~$ gcc clock.c -o clock_x86 -static -lrt
vagrant@vagrant-ubuntu-trusty-64:~$ ./clock_x86
0 s, 221 ns
vagrant@vagrant-ubuntu-trusty-64:~$ mipsel-openwrt-linux-gcc clock.c -lrt -static -o clock_mips
<...scp...>
root@OpenWrt:~# ./clock_mips
0 s, 3000 ns
我对时钟、MIPS、处理器等有什么误解吗?无论处理器架构如何,我都希望调用 CLOCK_REALTIME 以产生纳秒级粒度/分辨率。
此外,如果有人能阐明这些 POSIX 计时器是如何实现的以及它们是如何获得测量值的,我将不胜感激。
【问题讨论】:
标签: c posix mips timing time.h