在C ++中,时钟稳定状态编译器依赖?

时间:2016-01-16 05:21:48

标签: c++

我使用以下代码,输出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

为什么会有差异?

1 个答案:

答案 0 :(得分:2)

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

  

20.12.7.3班级high_resolution_clock

     

high_resolution_clock的对象表示具有最短刻度周期的时钟。 high_resolution_clock可能是system_clocksteady_clock的同义词。

例如,Visual C++ 2015将typedef改为steady_clock,英特尔也可能。{/ p>

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