在 C++ 中以微秒分辨率测量时间?

发布于 2024-10-07 11:04:19 字数 82 浏览 7 评论 0原文

我正在寻找一种在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

奢华的一滴泪 2024-10-14 11:04:19

使用 QueryPerformanceCounterQueryPerformanceFrequency 在 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).

面如桃花 2024-10-14 11:04:19

Windows 中有两种高精度(100 ns 分辨率)时钟:

QueryPerformanceCounter 独立于任何外部时间参考,并且不与其同步。它对于测量绝对时间跨度很有用。

GetSystemTimePreciseAsFileTime 已同步。如果您的 PC 正在加速或减慢时钟以使其逐渐与时间服务器同步,则 GetSystemTimePreciseAsFileTime 将适当地比绝对时间跨度更慢或更快。

指导是:

  • 如果您需要 UTC 同步时间戳,以便跨多个系统使用,例如:
  • 如果您只需要绝对时间跨度,请使用 GetSystemTimePreciseAsFileTime:使用 QueryPerformanceCounter

Bonus Reading

Windows 中的所有内核级跟踪基础设施都使用 QueryPerformanceCounter 来测量绝对时间跨度。

GetSystemTimeAsFileTime 对于日志记录之类的事情很有用。

There are two high-precision (100 ns resolution) clocks available in Windows:

QueryPerformanceCounter 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:

  • if you need UTC synchronized timestamps, for use across multiple systems for example: use GetSystemTimePreciseAsFileTime
  • if you only need absolute timespans: use QueryPerformanceCounter

Bonus Reading

All kernel-level tracing infrastructure in Windows use QueryPerformanceCounter for measuring absolute timespans.

GetSystemTimeAsFileTime would be useful for something like logging.

一曲琵琶半遮面シ 2024-10-14 11:04:19

http://www.boost.org/doc/libs /1_45_0/doc/html/date_time/posix_time.html

尽管如此

使用亚秒分辨率时钟获取 UTC 时间。在 Unix 系统上,这是使用 GetTimeOfDay 实现的。在大多数 Win32 平台上,它是使用 ftime 实现的。 Win32 系统通常无法通过此 API 实现微秒分辨率。如果更高的分辨率对您的应用程序至关重要,请测试您的平台以查看所达到的分辨率。

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.

茶花眉 2024-10-14 11:04:19

我想已经给出的 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.

这个俗人 2024-10-14 11:04:19

最近的实现可以在 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

零度℉ 2024-10-14 11:04:19

(因为还没有人提到纯 C++ 方法),

从 c++11 开始:

#include <chrono>
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch())

获取自 1970-01-01 以来的微秒数,以及 php 的 microtime(true) 端口api 将为

#include <chrono>
double microtime(){
    return (double(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count()) / double(1000000));
}

您提供自 1970-01-01 以来的秒数,精度为微秒

(since no-one has mentioned a pure c++ approach yet),

as of c++11:

#include <chrono>
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch())

gets you the number of microseconds since 1970-01-01, and a port of php's microtime(true) api would be

#include <chrono>
double microtime(){
    return (double(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count()) / double(1000000));
}

gets you the number of seconds since 1970-01-01 with microsecond precision

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文