在 C++ 中以微秒分辨率测量时间?
我正在寻找一种在 C++/Windows 中测量微秒的方法。
我读到了“时钟”函数,但它只返回毫秒......
有办法做到吗?
I'm looking for a way to measure microsecs in C++/Windows.
I read about the "clock" function, but it returns only milliseconds...
Is there a way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 QueryPerformanceCounter 和 QueryPerformanceFrequency 在 Windows 上实现最精细的粒度计时。
有关使用这些 API 进行代码计时的 MSDN 文章此处(示例代码是用 VB 编写的 - 抱歉)。
Use QueryPerformanceCounter and QueryPerformanceFrequency for finest grain timing on Windows.
MSDN article on code timing with these APIs here (sample code is in VB - sorry).
Windows 中有两种高精度(100 ns 分辨率)时钟:
GetSystemTimePreciseAsFileTime
:100ns 分辨率,与 UTC 同步QueryPerformanceCounter
:100ns 分辨率,未与 UTC 同步QueryPerformanceCounter 独立于任何外部时间参考,并且不与其同步。它对于测量绝对时间跨度很有用。
GetSystemTimePreciseAsFileTime 已同步。如果您的 PC 正在加速或减慢时钟以使其逐渐与时间服务器同步,则 GetSystemTimePreciseAsFileTime 将适当地比绝对时间跨度更慢或更快。
指导是:
Bonus Reading
Windows 中的所有内核级跟踪基础设施都使用 QueryPerformanceCounter 来测量绝对时间跨度。
GetSystemTimeAsFileTime 对于日志记录之类的事情很有用。
There are two high-precision (100 ns resolution) clocks available in Windows:
GetSystemTimePreciseAsFileTime
: 100ns resolution, synchronized to UTCQueryPerformanceCounter
: 100ns resolution, not synchronized to UTCQueryPerformanceCounter is independant of, and isn't synchronized to, any external time reference. It is useful for measuring absolute timespans.
GetSystemTimePreciseAsFileTime is 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.
The guidance is:
Bonus Reading
All kernel-level tracing infrastructure in Windows use QueryPerformanceCounter for measuring absolute timespans.
GetSystemTimeAsFileTime would be useful for something like logging.
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
我想已经给出的 QuerPerformance* 答案没有任何问题:问题是针对特定于 Windows 的解决方案,就是这样。对于跨平台 C++ 解决方案,我想 boost::chrono 最有意义。 Windows 实现使用 QuerPerformance* 方法,您也可以立即拥有 Linux 和 Mac 解决方案。
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::chrono makes most sense. The Windows implementation uses the QuerPerformance* methods, and you immediately have a Linux and Mac solution too.
最近的实现可以在 Windows 上提供微秒分辨率的时间戳
具有高精度。系统文件时间和性能计数器的联合使用允许
这样的准确度请参阅此帖子或这个
最近的实现之一可以在 Windows 时间戳项目
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 thread or also this one
One of the recent implementations can be found at the Windows Timestamp Project
(因为还没有人提到纯 C++ 方法),
从 c++11 开始:
获取自 1970-01-01 以来的微秒数,以及 php 的
microtime(true)
端口api 将为您提供自 1970-01-01 以来的秒数,精度为微秒
(since no-one has mentioned a pure c++ approach yet),
as of c++11:
gets you the number of microseconds since 1970-01-01, and a port of php's
microtime(true)
api would begets you the number of seconds since 1970-01-01 with microsecond precision