windows 在 C++ 中以微秒的分辨率测量时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4444090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Measuring time with a resolution of microseconds in C++?
提问by Idov
I'm looking for a way to measure microsecs in C++/Windows.
我正在寻找一种在 C++/Windows 中测量微秒的方法。
I read about the "clock" function, but it returns only milliseconds...
Is there a way to do it?
我读到了“时钟”函数,但它只返回毫秒......
有没有办法做到这一点?
回答by Steve Townsend
Use QueryPerformanceCounterand QueryPerformanceFrequencyfor finest grain timing on Windows.
使用QueryPerformanceCounter和QueryPerformanceFrequency在 Windows 上获得最精细的时序。
MSDN article on code timing with these APIs here(sample code is in VB - sorry).
与这些API代码时序MSDN文章在这里(示例代码是在VB -对不起)。
回答by Ian Boyd
There are two high-precision (100 ns resolution) clocks available in Windows:
Windows 中有两个高精度(100 ns 分辨率)时钟:
GetSystemTimePreciseAsFileTime
: 100ns resolution, synchronized to UTCQueryPerformanceCounter
: 100ns resolution, notsynchronized to UTC
GetSystemTimePreciseAsFileTime
: 100ns 分辨率,与 UTC 同步QueryPerformanceCounter
: 100ns 分辨率,与 UTC不同步
QueryPerformanceCounteris independant of, and isn't synchronized to, any external time reference. It is useful for measuring absolute timespans.
QueryPerformanceCounter与任何外部时间参考无关,也不同步。它对于测量绝对时间跨度很有用。
GetSystemTimePreciseAsFileTimeis synchronized. If your PC is in the process of speeding up, or slowing down, your clock to bring it gradually into sync with a time server, GetSystemTimePreciseAsFileTime will appropriately be slower or faster than absolute timespans.
GetSystemTimePreciseAsFileTime已同步。如果您的 PC 正在加速或减速,您的时钟使其逐渐与时间服务器同步,GetSystemTimePreciseAsFileTime 将适当地比绝对时间跨度更慢或更快。
The guidance is:
指导意见是:
- if you need UTC synchronized timestamps, for use across multiple systems for example: use GetSystemTimePreciseAsFileTime
- if you only need absolute timespans: use QueryPerformanceCounter
- 如果您需要 UTC 同步时间戳,以便跨多个系统使用,例如:使用 GetSystemTimePreciseAsFileTime
- 如果您只需要绝对时间跨度:使用 QueryPerformanceCounter
Bonus Reading
奖励阅读
- MSDN: Acquiring high-resolution time stamps
- MSDN: QueryPerformanceCounter function
- MSDN: GetSystemTimePreciseAsFileTime function
- MSDN: GetSystemTimeAdjustment function(where you can see if Windows is currently running your clock faster or slower in order to catch up to current true UTC time)
- MSDN:获取高分辨率时间戳
- MSDN:QueryPerformanceCounter 函数
- MSDN:GetSystemTimePreciseAsFileTime 函数
- MSDN:GetSystemTimeAdjustment 函数(您可以在其中查看 Windows 当前是否正在更快或更慢地运行您的时钟以赶上当前的真实 UTC 时间)
All kernel-level tracing infrastructure in Windows use QueryPerformanceCounter for measuring absolute timespans.
Windows 中的所有内核级跟踪基础结构都使用 QueryPerformanceCounter 来测量绝对时间跨度。
GetSystemTimeAsFileTime would be useful for something like logging.
GetSystemTimeAsFileTime 对于日志之类的东西很有用。
回答by Anycorn
http://www.boost.org/doc/libs/1_45_0/doc/html/date_time/posix_time.html
http://www.boost.org/doc/libs/1_45_0/doc/html/date_time/posix_time.html
altough
虽然
Get the UTC time using a sub second resolution clock. On Unix systems this is implemented using GetTimeOfDay. On most Win32 platforms it is implemented using ftime. Win32 systems often do not achieve microsecond resolution via this API. If higher resolution is critical to your application test your platform to see the achieved resolution.
使用亚秒分辨率时钟获取 UTC 时间。在 Unix 系统上,这是使用 GetTimeOfDay 实现的。在大多数 Win32 平台上,它是使用 ftime 实现的。Win32 系统通常无法通过此 API 实现微秒分辨率。如果更高的分辨率对您的应用程序至关重要,请测试您的平台以查看达到的分辨率。
回答by Arno
More recent implementations can provide microsecond resolution timestamps on windows with high accuracy. The joint use of system filetime and performance counter allows such accuracies see this threador also this one
最近的实现可以在窗口上提供高精度的微秒分辨率时间戳。联合使用的系统文件时间和性能计数器允许这样的精度看到这个线程或也这一个
One of the recent implementations can be found at the Windows Timestamp Project
最近的实现之一可以在Windows Timestamp Project 中找到
回答by ecotax
I guess there's nothing wrong with the QuerPerformance* answer already given: the question was for a Windows-specific solution, and this is it. For a cross-platform C++ solution, I guess boost::chronomakes most sense. The Windows implementation uses the QuerPerformance* methods, and you immediately have a Linux and Mac solution too.
我想已经给出的 QuerPerformance* 答案没有错:问题是针对特定于 Windows 的解决方案,就是这样。对于跨平台的 C++ 解决方案,我想boost::chrono最有意义。Windows 实现使用 QuerPerformance* 方法,您也立即拥有 Linux 和 Mac 解决方案。