gpt4 book ai didi

c++ - 在 C++ 中,时钟稳定状态是否依赖于编译器?

转载 作者:行者123 更新时间:2023-11-30 03:45:22 25 4
gpt4 key购买 nike

我用下面的代码,输出3个时钟的稳定状态

#include <chrono>
#include <iostream>
#include <iomanip>

template <typename C>
void printClockData ()
{
using namespace std;

cout << "- precision: ";
// if time unit is less or equal one millisecond
typedef typename C::period P;// type of time unit
if (ratio_less_equal<P,milli>::value) {
// convert to and print as milliseconds
typedef typename ratio_multiply<P,kilo>::type TT;
cout << fixed << double(TT::num)/TT::den
<< " milliseconds" << endl;
}
else {
// print as seconds
cout << fixed << double(P::num)/P::den << " seconds" << endl;
}
cout << "- is_steady: " << boolalpha << C::is_steady << endl;
}

int main()
{
std::cout << "system_clock: " << std::endl;
printClockData<std::chrono::system_clock>();
std::cout << "\nhigh_resolution_clock: " << std::endl;
printClockData<std::chrono::high_resolution_clock>();
std::cout << "\nsteady_clock: " << std::endl;
printClockData<std::chrono::steady_clock>();
}

用g++编译,输出

enter image description here

使用 intel icl 输出编译

enter image description here

为什么不同?

最佳答案

是的,对于 high_resolution_clock。正如 C++ 标准所说:

20.12.7.3 Class high_resolution_clock

Objects of class high_resolution_clock represent clocks with the shortest tick period. high_resolution_clock may be a synonym for system_clock or steady_clock.

例如Visual C++ 2015是否已将其类型定义为 steady_clock,Intel 可能也是。

对于 steady_clock,标准明确将 is_steady 设置为 true。

关于c++ - 在 C++ 中,时钟稳定状态是否依赖于编译器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34823811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com