【发布时间】:2013-04-22 22:19:32
【问题描述】:
我有这个程序可以打印 2 个不同实例之间的时间差,但它的打印精度为秒。我想以毫秒为单位打印它,另一个以纳秒为单位打印。
//Prints in accuracy of seconds
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now, later;
double seconds;
time(&now);
sleep(2);
time(&later);
seconds = difftime(later, now);
printf("%.f seconds difference", seconds);
}
我怎样才能做到这一点?
【问题讨论】:
-
你在什么平台上?
-
@interjay:对不起,我应该提一下,它是 Linux。 gcc 编译器。
-
你不能有纳秒的精度。 stackoverflow.com/questions/13610471/…
-
@georgesl 你可以在 Linux 上获得纳秒精度的时间。尽管我相信它可能是基于大多数架构上 CPU 的内部时钟的两个微秒值之间的插值,但这并不能保证是纳秒精度。不过,您始终可以使用
clock_getres找到它。