std::chrono::duration_cast
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
template <class ToDuration, class Rep, class Period> constexpr ToDuration duration_cast(const duration<Rep,Period>& d); |
(C++11 起) | |
std::chrono::duration转换到不同类型的
ToDuration
时间.原文:
Converts a std::chrono::duration to a duration of different type
ToDuration
.没有使用隐式转换。的乘法和除法,避免在可能的情况下,如果它是已知在编译时,一个或多个参数是1。完成在最广泛的类型的可用的计算和转换为仅完成时的结果类型.
原文:
No implicit conversions are used. Multiplications and divisions are avoided where possible, if it is known at compile time that one or more parameters are 1. Computations are done in the widest type available and converted to the result type only when finished.
目录 |
[编辑] 参数
d | - | 时间转换
|
[编辑] 返回值
d
转换的持续时间的类型ToDuration
.原文:
d
converted to a duration of type ToDuration
.[编辑] 注释
该函数不参与重载决议,除非
ToDuration
是一个实例std::chrono::duration.原文:
The function does not participate in the overload resolution unless
ToDuration
is an instance of std::chrono::duration.含蓄可以进行浮点持续时间或持续时间之间的整数间的转换,源周期整除的目标期限(例如几小时缩短到几分钟),没有duration_cast.
原文:
Casting between floating-point durations or between integer durations where the source period is exactly divisible by the target period (e.g. hours to minutes) can be performed 含蓄, no duration_cast is needed.
[编辑] 示例
此示例测量一个函数的执行时间
原文:
This example measures the execution time of a function
#include <iostream> #include <chrono> #include <thread> void f() { std::this_thread::sleep_for(std::chrono::seconds(1)); } int main() { auto t1 = std::chrono::high_resolution_clock::now(); f(); auto t2 = std::chrono::high_resolution_clock::now(); std::cout << "f() took " << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() << " milliseconds\n"; }
输出:
f() took 1000 milliseconds