[英]How can I know my `std::chrono::high_resolution_clock` correspond to the alias of `steady_clock` or `system_clock`, or other?
How can I know my std::chrono::high_resolution_clock
correspond to the alias of steady_clock
or system_clock
, or other?我怎么知道我的
std::chrono::high_resolution_clock
steady_clock
std::chrono::high_resolution_clock
对应于steady_clock
或system_clock
的别名,或其他?
Many websites and books are providing examples using the system_clock
, that might be goes back due to the synchronization, to measure the time spent for a function.许多网站和书籍都提供了使用
system_clock
示例,这可能由于同步而返回,以测量函数花费的时间。 I don't think that is the good idea to measure the time.我认为这不是衡量时间的好主意。 Instead, I think we should use
std::chrono::steady_clock
.相反,我认为我们应该使用
std::chrono::steady_clock
。
Sometimes std::chrono::high_resolution_clock
is used.有时使用
std::chrono::high_resolution_clock
。 According to the URL https://en.cppreference.com/w/cpp/chrono/high_resolution_clock , it says "It may be an alias of std::chrono::system_clock
or std::chrono::steady_clock
, or a third, independent clock."根据 URL https://en.cppreference.com/w/cpp/chrono/high_resolution_clock ,它说“它可能是
std::chrono::steady_clock
std::chrono::system_clock
或std::chrono::steady_clock
,或者第三个,独立时钟。”
So, I want to know how can I check which aliases my std::chrono::high_resolution_clock
correspond to.所以,我想知道如何检查
std::chrono::high_resolution_clock
对应的别名。 Any ideas?有任何想法吗?
Directly from Sam Varshavchik's comment.直接来自 Sam Varshavchik 的评论。 You can use
std::is_same
from <type_traits>
to check if two types are the same.您可以使用
<type_traits>
std::is_same
来检查两种类型是否相同。
Here's an example for checking the three standard clocks in chrono
:这是检查
chrono
三个标准时钟的示例:
#include <chrono>
#include <iostream>
#include <type_traits>
int main() {
using namespace std::chrono;
std::cout << std::boolalpha
<< std::is_same_v<system_clock, high_resolution_clock> << '\n'
<< std::is_same_v<system_clock, steady_clock> << '\n'
<< std::is_same_v<high_resolution_clock, steady_clock> << '\n'
;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.