Get a timestamp in C in microseconds?

前端 未结 7 1011
庸人自扰
庸人自扰 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 01:57

    struct timeval contains two components, the second and the microsecond. A timestamp with microsecond precision is represented as seconds since the epoch stored in the tv_sec field and the fractional microseconds in tv_usec. Thus you cannot just ignore tv_sec and expect sensible results.

    If you use Linux or *BSD, you can use timersub() to subtract two struct timeval values, which might be what you want.

提交回复
热议问题