std :: chrono似乎没有提供准确的时钟分辨率/频率 [英] std::chrono doesn't seem to be giving accurate clock resolution/frequency

查看:91
本文介绍了std :: chrono似乎没有提供准确的时钟分辨率/频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关此内容,但这些数字并没有加起来。在我的操作系统(Windows)上,我可以像这样检查系统时钟的分辨率:

I've been reading up about this, and the numbers don't add up. On my operating system (Windows), I can check the resolution of the system clock like this:

LARGE_INTEGER largeInt; // Basically a struct/union containing a long long
QueryPerformanceFrequency(&largeInt);
// largeInt now holds the frequency of my system's clock, it's 3903987
// This ends up being a resolution of about 256 nanoseconds

一切都很好,但是现在我想使用std :: chrono来检查相同的细节。根据cppreference.com和该站点上的一个流行答案,std :: chrono时钟的周期是一个编译时比率,由分子和分母组成,指定了刻度之间的秒数。

All is good, but now I want to use std::chrono to check the same details. According to cppreference.com and a popular answer on this site, the period of a std::chrono clock is a compile-time ratio consisting of a numerator and denominator which specifies how many seconds there are between ticks.

cppreference.com:

cppreference.com:


时期:a
std :: ratio表示刻度周期(即每个
滴答声的秒数)

period: a std::ratio representing the tick period(i.e.the number of seconds per tick)

然后从堆栈溢出答案中:

And from the a stack overflow answer:


最小可表示持续时间为
high_resolution_clock :: period :: num /
high_resolution_clock :: period :: den秒。您可以像
这样打印它:

The minimum representable duration is high_resolution_clock::period::num / high_resolution_clock::period::den seconds. You can print it like this:



std::cout <<
(double)std::chrono::high_resolution_clock::period::num /   
std::chrono::high_resolution_clock::period::den;

所以我尝试了:

// On Windows the result is typedeffed as intmax_t, which is a typedef for a long long
    intmax_t numerator = std::chrono::high_resolution_clock::period::num;
    intmax_t denominator = std::chrono::high_resolution_clock::period::den;
    // numerator is 1 and denominator is one billion, which would suggest a 
    // tick period of one nanosecond?

根据解释,我的系统时钟能够具有1纳秒的分辨率?操作系统似乎并不这么认为。我还尝试了其他一些工具来支持我的系统的时钟频率在3903987左右,因此我看不出它如何能够管理比这个更好的分辨率。

According to what's been explained my system's clock is capable of one nanosecond resolution? The OS doesn't seem to think so. I've also tried some other tools that support that my system's clock frequency is around 3903987, and so I don't see how it could manage a resolution finer than that.

我不确定频率是否随CPU功率设置/增强模式而变化,尽管high_resolution_clock :: is_steady结果为true。我重新启动了计算机两次,并得到了3903994和3903991的频率计数器,因此每次启动该数字时似乎都会改变。我想这不是在开始运行程序时计算时钟的理想方法。

I'm not sure whether the frequency changes with CPU power settings / boost mode or not, though high_resolution_clock::is_steady results as true. I restarted my computer twice and got frequency counters of 3903994 and 3903991, so each time booting up this number seems to change. I guess this wouldn't be ideal to calculate the clock at compile time, as opposed to the start of running the program.

那么有没有办法获得实际的分辨率/ std :: chrono的系统时钟频率?

So is there way to get the actual resolution/frequency of your system clock with std::chrono?

推荐答案

period :: num period :: den 只是告诉您HPC所在的单位。

period::num and period::den just tell you what units HPC is in.

在您的系统上,它的单位是纳秒。 滴答不是时钟增加的单位,而是时钟时间测量的单位。

On your system, its units are nanoseconds. The "tick" isn't the unit that the clock increases by, but rather the unit the clock time is measured in.

不是衡量处理器周期,而是衡量纳秒。

It does not measure "processor cycles", it measures nanoseconds. Its job is to relatively consistently give you the current time in nanoseconds.

现在,如果CPU每256纳秒运行一次,则两次调用 high_resolution_clock :: now()和另一个相差不远(因为它们需要多个周期!)。

Now, if the CPU is running at one cycle every 256 nanoseconds, then two calls to high_resolution_clock::now() and another won't be less than that apart (as they take more than one cycle!).

C ++不会公开您的CPU的时钟时间。

C++ does not expose the clock time of your CPU. It isn't all that useful.

如果要记录两个时间点,请使用 clock :: now()为您的时钟。如果您想要std库可以实现的最佳分辨率,请使用 high_performance_clock 。如果保证100%的时钟时间从不倒退,请使用 steady_clock (在检查其分辨率是否足够之后)。

If you want to record two time points, use clock::now() for your clock. If you want the best resolution the std library can pull off, use high_performance_clock. If having clock times that are 100% guaranteed to never go backwards, use steady_clock (after checking that its resolution is sufficient).

您可以将这些时间点转换为系统时钟,如果需要日历时间,可以将其转换为time_t。 (根据我的经验,在时钟之间进行转换需要您在这两个时间点上都有一个共同的时间点;您应该一次生成这个共同的时间点并重复使用)。

You can convert these time points into system clock, and from that to go time_t if you want calendar time. (Converting between clocks requires you have a common time point in both in my experience; you should generate this common time point once and reuse it)

如果您需要的分辨率比在系统时钟允许的情况下,您可以将时间点转换为系统时钟,将其转换为time_t,再转换为系统时钟,再转换回您的时钟,减去差,然后以纳秒为单位显示该差。

If you need more resolution than system clock permits, you can convert time points to system clock, that to time_t, that back to system clock and back to your clock, subtract the difference, and display that difference in a unit like nanoseconds.

此处

chrono主要提供的是一个库,可轻松使用不同的时钟系统并确保输入的安全性。它具有持续时间和时间点,每个时间点和时间点都随身携带单位,因此不会意外增加秒为纳秒。

What chrono provides mainly is a library to make working with different clock systems easy and type safe. It has durations and time points, each of which carry their units with them so accidentally adding seconds-as-nanoseconds won't happen.

这篇关于std :: chrono似乎没有提供准确的时钟分辨率/频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆