【发布时间】:2012-01-13 05:47:41
【问题描述】:
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吗? -
@Yappie:部分,但如何从那里开始?
-
std::ratio基本上是用于编译时间算术的编译时间值。它没有实例成员,只有静态 constexpr 成员,因此“初始化”比率没有任何意义。如果时钟周期小于或等于一毫秒,您可以执行std::ratio_less_equal<std::milli,std::high_resolution_clock::period>::value之类的操作,这将是true。 -
只是想说明@bames53 已交换操作数。以防万一有人不熟悉 std::ratio_less_equal 并对语义感到疑惑(比如我)。
标签: c++ c++11 clock duration chrono