部分GNU代码片 9、当前时间 精确到us

来源:互联网 发布:javascript初学者 编辑:程序博客网 时间:2024/04/27 20:30

static inline double time_cost(void)
{
#ifdef HAVE_CLOCK_GETTIME
    struct timespec ts;
   
    assert(clock_gettime(CLOCK_REALTIME, &ts) == 0);
    return (ts.tv_sec + 1e-9 * ts.tv_nsec);
#else
    struct timeval tv;

    assert(gettimeofday(&tv, NULL) == 0);
    return (tv.tv_sec + 1e-6 * tv.tv_usec);
#endif

 

编译的时候加上 -lrt