Get a timestamp in C in microseconds?

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

    You need to add in the seconds, too:

    unsigned long time_in_micros = 1000000 * tv.tv_sec + tv.tv_usec;
    

    Note that this will only last for about 232/106 =~ 4295 seconds, or roughly 71 minutes though (on a typical 32-bit system).

提交回复
热议问题