C++ 保存时间到微秒0.1us

2021-05-10  本文已影响0人  Kerwin_H
#ifdef _WIN32

#include <windows.h>

#else

#include <time.h>

#endif  // _WIND32

int64_t GetSysTimeMicros()

{

#ifdef _WIN32

// 从1601年1月1日0:0:0:000到1970年1月1日0:0:0:000的时间(单位100ns)

#define EPOCHFILETIME  (116444736000000000UL)

FILETIME ft;

LARGE_INTEGER li;

int64_t tt = 0;

GetSystemTimeAsFileTime(&ft);

li.LowPart = ft.dwLowDateTime;

li.HighPart = ft.dwHighDateTime;

// 从1970年1月1日0:0:0:000到现在的微秒数(UTC时间)

tt = (li.QuadPart - EPOCHFILETIME);// / 10;

return tt;

#else

timeval tv;

gettimeofday(&tv, 0);

return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;

#endif // _WIN32

return 0;

}

int main() {

while (true) {

printf("time %lld us\n", GetSysTimeMicros());

}

return 1;

}
上一篇 下一篇

猜你喜欢

热点阅读