如何获得high_resolution_clock的精度? [英] How to get the precision of high_resolution_clock?
问题描述
C ++ 11定义 high_resolution_clock
,它具有 period
和
。但我不知道如何获得该时钟的精确。
或者,如果我不能得到精度,我不知何故至少得到一个计数在最小可表示持续时间之间的纳秒?可能使用 period
?
#include< iostream>
#include< chrono>
void printPrec(){
std :: chrono :: high_resolution_clock :: rep x = 1;
//这不是初始化'period'的正确方法:
// high_resolution_clock :: period y = 1;
std :: cout<< 最小周期是
< / *在这里用'x'或'y'做什么? * /
<< nanos\\\
;
}
code> 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
的一种特殊化,它是一个在编译时表示比率的模板。它提供了两个积分常数: num
和 den
,分别是分数的分子和分母。
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";
}
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.
这篇关于如何获得high_resolution_clock的精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!