如何获得high_resolution_clock的精度?
C++11 定义了 high_resolution_clock
,它具有成员类型 period
和 rep
。但我不知道如何获得该时钟的精度。
或者,如果我可能无法达到精度,我是否可以至少以纳秒为单位获得最小可表示持续时间之间的计数?可能使用句号
?
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最小可表示持续时间为
high_resolution_clock::period::num / high_resolution_clock::period::den
秒。你可以这样打印:这是为什么?时钟的
::period
成员定义为“时钟的滴答周期(以秒为单位)”。它是std::ratio
的特化,它是在编译时表示比率的模板。它提供了两个整数常量:num
和den
,分别是分数的分子和分母。The minimum representable duration is
high_resolution_clock::period::num / high_resolution_clock::period::den
seconds. You can print it like this:Why is this? A clock's
::period
member is defined as "The tick period of the clock in seconds." It is a specialization ofstd::ratio
which is a template to represent ratios at compile-time. It provides two integral constants:num
andden
, the numerator and denominator of a fraction, respectively.我对 R. Martinho Fernandes 的回答投了赞成票,因为我相信它为这个问题提供了最清晰、最直接的答案。不过,我想添加一些代码来显示更多
功能,并解决OP问题的这一部分:将这么多信息放入评论中是不切实际的。但我认为这个答案是对 R. Martinho Fernandes 答案的支持性评论。
首先是代码,然后是解释:
首先,我创建了一个使用
double
作为表示形式的nanosecond
(NS
)。我使用了 double 以防万一我需要显示纳秒的分数(例如 0.5 ns)。接下来,每个时钟都有一个名为
duration
的嵌套类型。这是一个chrono::duration
,它将具有相同的std::ratio
,因此具有相同的num
和den 正如 R. Martinho Fernandes 的回答中所指出的。其中一个
duration
转换为NS
将为我们提供Clock
的一个时钟周期中有多少纳秒。可以使用count()
成员函数从duration
中提取该值。对我来说,这个程序打印出:
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: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:
First I created a
nanosecond
that is using adouble
as the representation (NS
). I useddouble
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 achrono::duration
that will have the samestd::ratio
, and thus the samenum
andden
as pointed out in R. Martinho Fernandes's answer. One of thoseduration
s, converted toNS
will give us how many nanoseconds in one clock tick ofClock
. And that value can be extracted from theduration
with thecount()
member function.For me this program prints out:
表示时钟滴答周期的
std::ratio
类型,以秒为单位。在命名空间std::chrono
中定义An
std::ratio
type representing the tick period of the clock, in seconds.Defined in namespacestd::chrono