C++11定义了high_resolution_clock
,它具有period
和rep
成员类型。但我无法确定如何获取该钟表的精度。
如果可能的话,我是否可以通过使用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";
}
std::ratio
吗? - Yappiestd::ratio
基本上是用于编译时算术的编译时值。它没有实例成员,只有静态 constexpr 成员,因此“初始化”比率没有任何意义。您可以执行诸如std::ratio_less_equal<std::milli,std::high_resolution_clock::period>::value
的操作,如果时钟周期小于或等于一毫秒,则该操作将为true
。 - bames53