关于< chrono>头在C ++ 11 [英] Several questions about <chrono> header in C++ 11

查看:253
本文介绍了关于< chrono>头在C ++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++ 11中的新< chrono> 标题有几个问题。使用Windows 7,Visual Studio 2012。



查看示例 http://en.cppreference.com/w/cpp/chrono

  #include< iostream> 
#include< chrono>
#include< ctime>

int fibonacci(int n)
{
if(n <3)return 1;
return fibonacci(n-1)+ fibonacci(n-2);
}

int main()
{
std :: chrono :: time_point< std :: chrono :: system_clock>开始,结束
start = std :: chrono :: system_clock :: now();
int result = fibonacci(42);
end = std :: chrono :: system_clock :: now();

int elapsed_seconds = std :: chrono :: duration_cast< std :: chrono :: seconds>
(end-start).count();
std :: time_t end_time = std :: chrono :: system_clock :: to_time_t(end);

std :: cout<< 完成计算在< std :: ctime(& end_time)
<< 经过时间:< elapsed_seconds<< s\\\
;
}

可能的输出

 已于6月16日完成计算20:42:57 2012 
已用时间:3s




  1. 我注意到该示例使用 std :: chrono :: system_clock :: now(); 意味着它可以用来测量只流逝的时间,而不是CPU时间???如果我想测量CPU时间,我应该使用什么时钟?

  2. 请注意,已用时间:3s 的输出会舍入为整数。

  3. p>正确



    根据标准:


    system_clock表示[s]从系统范围的实时时钟到挂钟时间。


    < chrono> library不提供测量CPU时间的机制,所以如果你想要你必须回到旧的< ctime> 库并使用 std :: clock()



    (如果你的目标是Windows,你必须回到平台特定的API Windows提供的CPU时间,因为你指出,他们 std :: clock()无法正常工作。



    system_clock 更像是 std :: time()的比较对象 std :: clock()。 (例如,注意 system_clock 提供 system_clock :: time_point s和 time_t 。)我想象在< chrono> 中没有时钟用于测量CPU时间是由于标准委员会的时间限制,该功能比系统的挂钟和实时时钟少。



    如果你想要CPU时间,但也希望< chrono> 提供,您应该实现符合标准中概述的时钟概念并提供CPU时间的时钟类型,可以使用 std :: clock() / code>。


  4. 的行


      int elapsed_seconds = std :: chrono :: duration_cast< std :: chrono :: seconds> 
    (end-start).count();


    是时间四舍五入为整数秒。您可以选择任何期限,也可以使用浮点数表示法,以允许非整数值:

      std :: int64_t elapsed_attoseconds = 
    std :: chrono :: duration_cast< std :: chrono :: duration< std :: int64_t,std :: atto>>
    (end-start).count();

    double elapsed_seconds =
    std :: chrono :: duration_cast< std :: chrono :: duration< double,std :: ratio< 1>>
    (end-start).count();请注意,在实际代码中,你应该避免使用 .count()

    来逃避 chrono :: duration 提供的强类型,直到你绝对必须。

      auto total_duration = end  -  start; 
    auto seconds = std :: chrono :: duration_cast< std :: chrono :: seconds>(total_duration);
    auto milli = std :: chrono :: duration_cast< std :: chrono :: milliseconds>(total_duration - seconds);

    std :: cout<< seconds.count()< s< milli.count()<< ms\\\
    ;



I have several questions about new <chrono> header in C++ 11. Using Windows 7, Visual Studio 2012.

Looking at the example http://en.cppreference.com/w/cpp/chrono

#include <iostream>
#include <chrono>
#include <ctime>

int fibonacci(int n)
{
    if (n < 3) return 1;
    return fibonacci(n-1) + fibonacci(n-2);
}

int main()
{
    std::chrono::time_point<std::chrono::system_clock> start, end;
    start = std::chrono::system_clock::now();
    int result = fibonacci(42);
    end = std::chrono::system_clock::now();

    int elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>
                             (end-start).count();
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);

    std::cout << "finished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds << "s\n";
}

Possible output

finished computation at Sat Jun 16 20:42:57 2012
elapsed time: 3s

  1. I have noticed that example uses std::chrono::system_clock::now(); does it mean it can be used to measure only elapsed time and not the CPU time ??? And if I want to measure CPU time, what Clocks should I use ?
  2. Notice that elapsed time: 3s is output is rounded to whole integer. Is there way to make it more granulated?

解决方案

  1. Correct

    According to the standard:

    system_clock represent[s] wall clock time from the system-wide realtime clock.

    The <chrono> library does not provide a mechanism for measuring CPU time, so if you want that you'll have to fall back on the old <ctime> library and use std::clock().

    (And if you're targeting Windows you'll have to fall back on whatever platform-specific API Windows provides for getting CPU time since, as you point out, their std::clock() doesn't work correctly.)

    system_clock is more like a counterpart to std::time() than to std::clock(). (E.g., note that system_clock provides conversions between system_clock::time_points and time_t.) I imagine that the lack of a clock in <chrono> for measuring CPU time is due to time constraints on the standard committee and the fact that that functionality is less used than the system's wall clock and real-time clocks.

    If you want CPU time but also want the benefits that <chrono> provides, you should implement a clock type that conforms to the Clock concept outlined in the standard and which provides CPU time, perhaps implemented internally using std::clock().

  2. The line that says

    int elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>
                        (end-start).count();
    

    is what causes the time to be rounded to an integral number of seconds. You can choose any period you'd like, or you can use a floating point representation in order to allow non-integral values:

    std::int64_t elapsed_attoseconds =
        std::chrono::duration_cast<std::chrono::duration<std::int64_t, std::atto>>
            (end-start).count();
    
    double elapsed_seconds =
        std::chrono::duration_cast<std::chrono::duration<double,std::ratio<1>>>
            (end-start).count();
    

    Note that in real code you should avoid using .count() to escape the strong typing provided by chrono::duration until you absolutely must.

    auto total_duration = end - start;
    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(total_duration);
    auto milli = std::chrono::duration_cast<std::chrono::milliseconds>(total_duration - seconds);
    
    std::cout << seconds.count() << "s " << milli.count() << "ms\n";
    

这篇关于关于&lt; chrono&gt;头在C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆