Get a timestamp in C in microseconds?

前端 未结 7 1010
庸人自扰
庸人自扰 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:00

    You have two choices for getting a microsecond timestamp. The first (and best) choice, is to use the timeval type directly:

    struct timeval GetTimeStamp() {
        struct timeval tv;
        gettimeofday(&tv,NULL);
        return tv;
    }
    

    The second, and for me less desirable, choice is to build a uint64_t out of a timeval:

    uint64_t GetTimeStamp() {
        struct timeval tv;
        gettimeofday(&tv,NULL);
        return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
    }
    

提交回复
热议问题