Get a timestamp in C in microseconds?

前端 未结 7 1017
庸人自扰
庸人自扰 2020-12-01 01:43

How do I get a microseconds timestamp in C?

I\'m trying to do:

struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_usec;

Bu

7条回答
  •  天涯浪人
    2020-12-01 02:03

    timespec_get from C11 returns up to nanoseconds, rounded to the resolution of the implementation.

    #include 
    struct timespec ts;
    timespec_get(&ts, TIME_UTC);
    struct timespec {
        time_t   tv_sec;        /* seconds */
        long     tv_nsec;       /* nanoseconds */
    };
    

    More details here: https://stackoverflow.com/a/36095407/895245

提交回复
热议问题