• boost 之计算机的时间-chrono


    chrono库位于名字空间boost ::chrono,需要包含头文件,为了使用扩展功能,需要定义宏 BOOST_CHRONO_EXTENSIONS,即:

    #define BOOST_ERROR_CODE_HEADER_ONLY        //无需编译 system

    #define BOOST_CHRONO_HEADER_ONLY                 //无需编译 chrono

    #define BOOST_CHRONO_EXTENSIONS                  //使用拓展功能

    #include<boost/chrono.hpp>                                        // chrono库文件

    时间长度

    chrono库定义了时间长度的表示 duration,在概念上与 date_time库的 time_duration相同,都表示一定长度的时间,但 duration侧重于编译期的时间单位表示,更接近ratio,所以与time_duration的接口非常不同。
     

    duration

    1. template<class Rep,class Period=ratio<1>>//默认模板参数 s
    2. class duration
    3. {
    4. public:
    5. typedef Rep rep;
    6. typedef Period period;
    7. private:
    8. rep rep_;
    9. public:
    10. constexpr duration();
    11. constexpr explicit duration(const Rep2&r);
    12. constexpr duration(const duration&d);
    13. ...
    14. }

    chrono库预定义了常用的时间单位,比如 hours.minutes.seconds.milliseconds等,但与date time库time duration使用派生子类的方式不同,chrono库通过在模板参数里使用不同的ratio来定义时间单位,因而每一个时间单位都是不同的类型:

    除了这些预定义的时间单位,我们也可以用typedef来简化 duration类型,实现任意勺时间分辨率,duration赋予了我们极大的灵活性。例如:
     

    1. typedef duration<long, std::ratio<30>> half_min;
    2. typedef duration<int, std::ratio<60*15>> quater;
    3. typedef duration<double, std::ratio<3600*24>> day;

    使用时间长度

    duration内部只有一个成员变量rep_,用来存储时间的计数,支持各种算术运算一因为本质上它就是一个算术类型。而时间单位则用ratio在编译期表示,各种时间单位的转换都是通过ratio在编译期模板元计算完成的(具体的实现原理我们暂不必深究)。

    duration 的构造函数指定它的时间计数,表示一段rep_*ratio秒的时间长度,成员函数count ()可以获得内部保存的时间计数rep_:

    类型转化:

    1. void cast()
    2. {
    3. duration<int, boost::ratio<1>> s(30);
    4. auto m=duration_castint,boost::ratio<60>>>(s);
    5. std::cout<
    6. }
    7. int main()
    8. {
    9. cast();
    10. }

    时钟

    时钟(Clock)是chrono里的另一个重要概念,它确定了一个时间的起点(since)和时间单位(duration),使用时钟我们就可以处理计算机世界里的时间。

    chrono库实现c++标准定义

    system_clock                                   :如实反映计算机实际时间的时钟;

    steady_clock                                   :稳定的时钟,不会因系统时间调整而变化;

    high_resolution_clock                      :高分辨率的时钟,但通常是前两者的typedef。

    度量运行时间

    process_real_cpu_clock:               进程执行的实际时间;

    process_user_cpu_clock:               用户CPU时间; 

    process_system_cpu_clock:           系统CPU时间;

    thread_clock                                    :线程执行的实际时间。

    各个时钟的接口基本相同 例如system_clock的类摘要

    1. class system_clock
    2. {
    3. public:
    4. typedef some_define duration;//时间单位
    5. typedef duration::rep rep;//计数单位
    6. typedef duration::period period;//ratio
    7. typedef chrono::time_point time_point;//时间点;
    8. ...
    9. }

    使用辅助类clock_string 的两个静态成员函数name ( )、since ()可以获取时钟的描述信息,它的声明是:

    1. template<class Clock class charT>
    2. {
    3. static std::basic_stringname();
    4. static std::basic_stringsince()
    5. }

    时间点

    时间点time _point 与时钟紧密关联,它必须由一个时钟产生,标记了自时钟起点以来所经过的时间。

    time_point的实例一般由now()产生 

    它内部持有duration变量 支持c++标准的加减和比较运算

    综合实例

    高精度计时器

    1. class steady_timer final
    2. {
    3. private:
    4. typedef boost::chrono::steady_clock clock_type;
    5. typedef clock_type::time_point time_point_type;
    6. typedef microseconds duration_type;
    7. time_point_type m_start=clock_type::now();//记录时间点
    8. public:
    9. steady_timer()=default;
    10. ~steady_timer()=default;
    11. public:
    12. void restart()
    13. {
    14. m_start=clock_type::now();
    15. }
    16. duration_type elapsed() const{
    17. return round(clock_type::now()-m_start);//四舍五入
    18. }
    19. };

    cup_timer

    为什么需要它

    cpu_timer使用了chrono库的高精度时钟 high_resolution_clock,不仅能够度量进程使用的实际时间,还能够度量CPU时间,它支持最高到微秒精度的计时,而且使用起来同样非常方便,是旧版本timer的很好替代品。

    是什么

    时间类型

    1. typedef boost::int_least64_t nanosecond_type;
    2. struct cpu_times
    3. {
    4. nanosecond_type wall; //挂钟日历
    5. nanosecond_type user; //用户cpu
    6. nanosecond_type system; //系统cpu
    7. };

    内部实现

    1. class cpu_timer
    2. {
    3. public:
    4. cpu_timer(){start();}
    5. bool is_stoped()const;
    6. cpu_times elapsed()const;
    7. ...
    8. //格式化 输出流逝时间
    9. std::string format(int placs,const std::string &format)const;
    10. std::string fromat(int places=default_places)const;
    11. ...
    12. }

    cpu_timer的实现原理与timer类似,只是内部改用chrono库的高精度时钟来获得时间计量,并且时间的计量也不是一个简单的整数类型 std::clock_t,而是改成了含有三个数值的类型cpu_times。

    cpu_times没有流输出功能,但提供格式化函数format (),可以把 elapsed ()的结界钴梅为个酊漆的字符电,形如

    xs wall, Ys user + Zs system = Cs CPU (P%) \n

    怎么用

    1. std::vectorv(10,"monada");
    2. cpu_timer t;
    3. assert(!t.is_stopped());
    4. for(int i=0;i<10000;++i)
    5. {
    6. v.push_back("nm");
    7. }
    8. t.stop();
    9. assert(t.is_stopped());
    10. std::cout<< "pause for a while. .." <
    11. std::cout << "we can do something ..." <
    12. t.resume();
    13. std:: cout<

    //定义默认的输出格式

    string default_fmt(" %ws wall,%us user + %ss system = %ts CPU (%p%)\n")

    其中的%格式化选项并不是printf标准,而是cpu timer库自定义的,含义是:

    %w:       挂钟时间,即cpu_times.wall值;

    %u        用户时间

    %s         系统时间       

    %t          总计cpu时间

    %p          总计时间占挂钟时间的百分比

    string format(const cpu_times& times,short places,const string& format);

    string format (const cpu_times& times,short places = default_places);

    实例用法:

    const nanoseconde_type ms=1000*1000;

    cpu_times ct=(2000*ms,1000*ms,100*ms);

    cout<

    运结果

    2.000s wall,1.000s user + 0.100s system = 1.100s cPU (55.0%)

    升级版本 auto_cpu_timer

    一句话 对象构建 自动计时 离开作用域 立即销毁

  • 相关阅读:
    威纶通触摸屏如何编写和调用宏指令进行逻辑判断
    次元裂缝已打开,AI绘画突飞猛进,其潜力究竟有多大
    前端学成在线项目详细解析三
    LeetCode:2385. 感染二叉树需要的总时间(DFS Java)
    kafka
    Linux常用命令
    原生JS中的Ajax
    web前端期末大作业:JavaScript大作业——福五鼠动漫网页制作(6页)带轮播图效果 学生个人单页面网页作业 学生网页设计成品 静态HTML网页单页制作
    【数学模拟卷总结】2023李林六套卷数学二第二套
    java基于springboot同城绘本馆图书借阅报名系统
  • 原文地址:https://blog.csdn.net/qq_62309585/article/details/126904626