获得时间点,微秒精度 [英] Getting time point with microseconds precision
问题描述
我需要以微秒的精度检索当前时间点。时间点可以是相对于任何固定日期。
I need to retrieve the current time point with a precision of microseconds. The time point can be relative to any fixed date.
如何实现?对于工作政策,我真的不应该使用 boost
或任何其他库。
How can it be achieved? For job policy, I really should't use boost
or any other lib.
多平台应用程序,在Linux下,我可以使用C ++ 11 system_clock :: now()。time_since_epoch()
,但在Windows下我使用VS2010, code> std :: chrono 库。
I'm working at a multiplatform application and under Linux, I can use C++11 system_clock::now().time_since_epoch()
, but under Windows I work with VS2010, so I have no std::chrono
library.
我看到了 RtlTimeToSecondsSince1970
函数,但其分辨率是第二个。
I've seen the RtlTimeToSecondsSince1970
function, but its resolution is a second.
推荐答案
计时器和计时是一个棘手的问题,平台实现不是很划算。所以我推荐一个适当的 #ifdef
的Windows的特定版本。
Timers and timing is a tricky enough subject that In my opinion current cross platform implementations are not quite up to scratch. So I'd recommend a specific version for windows with appropriate #ifdef
's. See other answers if you want a cross-platform version.
如果您已经/想使用windows特定的调用,则 GetSystemTimeAsFileTime
(或在Windows 8 GetSystemTimePreciseAsFileTime
)是获取UTC时间的最佳调用, QueryPerformanceCounter
有利于高分辨率时间戳。它将自1601年1月1日起的 100纳秒间隔数量返回到FILETIME结构中。
If you've got to/want to use a windows specific call then GetSystemTimeAsFileTime
(or on windows 8 GetSystemTimePreciseAsFileTime
) are the best calls for getting UTC time and QueryPerformanceCounter
is good for high resolution timestamps. It gives back the number of 100-nanosecond intervals since January 1, 1601 UTC into a FILETIME structure.
这篇精细的文章进入了测量的细节计时器和时间戳,非常值得一读。
This fine article goes into the gory details of measuring timers and timestamps in windows and is well worth a read.
编辑:转换一个FILETIME给我们,你需要通过ULARGE_INTEGER。
Converting a FILETIME to us, you need to go via a ULARGE_INTEGER.
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
ULARGE_INTEGER li;
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
unsigned long long valueAsHns = li.QuadPart;
unsigned long long valueAsUs = valueAsHns/10;
这篇关于获得时间点,微秒精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!