如何获得high_resolution_clock的精度?

发布于 2024-12-19 17:08:25 字数 620 浏览 0 评论 0原文

C++11 定义了 high_resolution_clock,它具有成员类型 periodrep。但我不知道如何获得该时钟的精度

或者,如果我可能无法达到精度,我是否可以至少以纳秒为单位获得最小可表示持续时间之间的计数?可能使用句号

#include <iostream>
#include <chrono>
void printPrec() {
    std::chrono::high_resolution_clock::rep x = 1;
    // this is not the correct way to initialize 'period':
    //high_resolution_clock::period y = 1;

    std::cout << "The smallest period is "
              << /* what to do with 'x' or 'y' here? */
              << " nanos\n";
}

C++11 defines high_resolution_clock and it has the member types period and rep. But I can not figure out how I can get the precision of that clock.

Or, if I may not get to the precision, can I somehow at least get a count in nanoseconds of the minimum representable time duration between ticks? probably using period?

#include <iostream>
#include <chrono>
void printPrec() {
    std::chrono::high_resolution_clock::rep x = 1;
    // this is not the correct way to initialize 'period':
    //high_resolution_clock::period y = 1;

    std::cout << "The smallest period is "
              << /* what to do with 'x' or 'y' here? */
              << " nanos\n";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

酒与心事 2024-12-26 17:08:26

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

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

这是为什么?时钟的 ::period 成员定义为“时钟的滴答周期(以秒为单位)”。它是 std::ratio 的特化,它是在编译时表示比率的模板。它提供了两个整数常量:numden,分别是分数的分子和分母。

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;

Why is this? A clock's ::period member is defined as "The tick period of the clock in seconds." It is a specialization of std::ratio which is a template to represent ratios at compile-time. It provides two integral constants: num and den, the numerator and denominator of a fraction, respectively.

从来不烧饼 2024-12-26 17:08:26

我对 R. Martinho Fernandes 的回答投了赞成票,因为我相信它为这个问题提供了最清晰、最直接的答案。不过,我想添加一些代码来显示更多 功能,并解决OP问题的这一部分:

我能以某种方式至少得到一个以纳秒为单位的最小值计数吗
刻度之间可表示的持续时间?

将这么多信息放入评论中是不切实际的。但我认为这个答案是对 R. Martinho Fernandes 答案的支持性评论。

首先是代码,然后是解释:

#include <iostream>
#include <chrono>

template <class Clock>
void
display_precision()
{
    typedef std::chrono::duration<double, std::nano> NS;
    NS ns = typename Clock::duration(1);
    std::cout << ns.count() << " ns\n";
}

int main()
{
    display_precision<std::chrono::high_resolution_clock>();
    display_precision<std::chrono::system_clock>();
}

首先,我创建了一个使用 double 作为表示形式的 nanosecond (NS)。我使用了 double 以防万一我需要显示纳秒的分数(例如 0.5 ns)。

接下来,每个时钟都有一个名为 duration 的嵌套类型。这是一个 chrono::duration,它将具有相同的 std::ratio,因此具有相同的 numden 正如 R. Martinho Fernandes 的回答中所指出的。其中一个 duration 转换为 NS 将为我们提供 Clock 的一个时钟周期中有多少纳秒。可以使用 count() 成员函数从 duration 中提取该值。

对我来说,这个程序打印出:

1 ns
1000 ns

I upvoted R. Martinho Fernandes's answer because I believe it offers the clearest, most straightforward answer to the question. However I wanted to add a little code that showed a little more <chrono> functionality and that addressed this part of the OP's question:

can I somehow at least get a count in nanoseconds of the minimum
representable time duration between ticks?

And it is impractical to put this much information into a comment. But I otherwise regard this answer as a supportive comment to R. Martinho Fernandes's answer.

First the code, and then the explanation:

#include <iostream>
#include <chrono>

template <class Clock>
void
display_precision()
{
    typedef std::chrono::duration<double, std::nano> NS;
    NS ns = typename Clock::duration(1);
    std::cout << ns.count() << " ns\n";
}

int main()
{
    display_precision<std::chrono::high_resolution_clock>();
    display_precision<std::chrono::system_clock>();
}

First I created a nanosecond that is using a double as the representation (NS). I used double just in case I needed to show fractions of a nanosecond (e.g. 0.5 ns).

Next, every clock has a nested type named duration. This is a chrono::duration that will have the same std::ratio, and thus the same num and den as pointed out in R. Martinho Fernandes's answer. One of those durations, converted to NS will give us how many nanoseconds in one clock tick of Clock. And that value can be extracted from the duration with the count() member function.

For me this program prints out:

1 ns
1000 ns
半暖夏伤 2024-12-26 17:08:26

表示时钟滴答周期的 std::ratio 类型,以秒为单位。在命名空间 std::chrono 中定义

template<intmax_t Num, intmax_t Denom = 1 > class ratio;

An std::ratio type representing the tick period of the clock, in seconds.Defined in namespace std::chrono

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