C中的System.nanoTime()等价物

时间:2010-02-19 11:43:04

标签: java c time

  

可能重复:
  C++ Timer function to provide time in nano seconds

我需要在接近3秒后退出循环,所以我需要计算经过的时间。

我正在将一些代码从Java转移到C,我在Java中使用简单的System.nanoTime(),

我如何在C中做到这一点?

我注意到时间(NULL)将返回秒数,但我正在寻找更高的精确度。

提前谢谢

2 个答案:

答案 0 :(得分:1)

对于您想要的分辨率,来自C标准库的clock()就足够了:

#include <time.h>
#define RUNTIME_MAX_SEC 3

clock_t start = clock();
while(clock() - start < CLOCKS_PER_SEC * RUNTIME_MAX_SEC)
{ ... }

答案 1 :(得分:0)

使用gettimeofday,它具有微秒分辨率。 Java中的System.nanoTime()通常使用* nixes上的gettimeofday

实现
相关问题