• 深入探究boost之timer库(1)


    1.timer

    1. class timer
    2. {
    3. private:
    4. std::clock_t _start_time;
    5. public:
    6. //std::clock():自进程启动以来的clock数
    7. //每秒的clock数由 CLOCKS_PER_SEC
    8. timer(){_start_time=std::clock();}
    9. void restart() {this->_start_timer=std::clock();}
    10. double elapsed() const
    11. {return douple(std::clock()-this->_start_time)/CLOCKS_PER_SEC;}
    12. duoble elapsed_min() const
    13. {return double(1)/double(CLOCKS_PER_SEC);}
    14. duoble elapsed_max() const
    15. {return (douple((std::numeroc_limitsclock_t>::max)())
    16. -double(_start_time))/double(CLOCKS_PER_SEC);}
    17. ~timer();
    18. };

    分析

    (1)基础概念:

    std::clock 自一个进程开始计时,每秒数由一个CLOCK_PER_SEC 定义

    std::numeric_limits 标准库的数值极限类 可以获取最大clock_t

     (2)

    void restart():重新计时

    douple elaped ()流逝的时间

    (3)简单应用:

    1. #include
    2. #include
    3. using namespace boost;
    4. int main()
    5. {
    6. boost::timer t;
    7. std::cout<<"max timespan:"
    8. <elapsed_max()/3600<<" h"<
    9. std::cout<<"min timespan:"
    10. <elapsed_min()<<" s"<
    11. std::cout<<"now time elapsed:"
    12. <elapsed()<
    13. }
    14. //结果:
    15. max timespan:2.56205e+09 h
    16. min timespan:1e-06 s
    17. now time elapsed:0

    (4)使用建议:

    适用于大部分要求不高的程序计时系统

    2.progress_timer

    顾名思义:继承timer的全部能力,担有简单的用法 只要声明对象就行啦

    例子:

    1. #include
    2. int main()
    3. {
    4. boost::progress_timer t;//声明对象开始计时
    5. //退出作用域 progress_timer
    6. }
    7. {
    8. progress_timer t;//第一个计时
    9. //do something
    10. }
    11. {
    12. progress_timer t;//第二个计时
    13. //do something
    14. }

    2.1类摘要

    progress_timer的摘要:

    1. class progress_timer:public timer,noncopyable
    2. {
    3. public:
    4. explicit progress_timer();
    5. prgress_timer(std::ostream& os);
    6. ~progress_timer();
    7. };

    唯一需要注意的是构造函数,它允许析构时的输出定向到指定的I/O流,默认是std::cout

    例子:

    1. 1 #include
    2. 2 #include
    3. 3 #include
    4. 4 #include
    5. 5 int main()
    6. 6 {
    7. 7 std::stringstream ss;
    8. 8 {
    9. 9 boost::progress_timer t(ss);
    10. 10
    11. 11 }
    12. 12 std::cout<str();
    13. 13 }
    14. 14

    3 progress_display

    目的:可以在控制台上显示程序执行进度

    3.1类摘要

    1. class progress_dispaly:boost noncopyable
    2. {
    3. public:
    4. progress_dispaly(unsigned long expected_count);
    5. progress_dispaly(unsigned long expected_count,
    6. std::ostream& os,
    7. const std::string & s1="\n",
    8. const std::string& s2="",
    9. const std::string& s3="");
    10. void restart(unsigned long expected_count);
    11. unsigned long operator+=(unsigned long increment);
    12. unsigned long operator++();
    13. unsigned long count() const;
    14. unsigned long expected_count() const;
    15. };

    progress_display 的构造函数接受一个long型的参数expected_count,表示用于进度显示的基数,是最常用的创建progress_display的方法。

    效果:

     3.2例子

    1. 1 #include
    2. 2 #include
    3. 3 #include
    4. 4 #include
    5. 5 #include
    6. 6 #include
    7. 7 int main()
    8. 8 {
    9. 9 std::vectorv(100,"aaa");
    10. 10 v[10]="";
    11. 11 v[23]="";
    12. 12 std::ofstream fs("./test.txt");
    13. 13
    14. 14 boost::progress_display pd(v.size());
    15. 15
    16. 16 for(auto pos=v.begin();pos!=v.end();++pos)
    17. 17 {
    18. 18 fs<<*pos<
    19. 19 ++pd;
    20. 20 if(pos->empty())
    21. 21 {
    22. 22 std::cout<<"null string #"
    23. 23 <<(pos-v.begin())<
    24. 24 }
    25. 25 }
    26. 26 }

    效果:

     这个显示混乱的问题很难解决,因为我们无法预知庞大的程序之中哪个地方会存在一个可能会干扰progress_display的输出。一个可能(但远非完美〉的办法是在每次显示进度时都调用restart()重新显示进度刻度,然后用operator+=来指定当前进度,而不是简单地调用operator++,例如:

    pd.restart(v.size())

    pd+=(pos-v.begin()+1)

     关注我一起学习

  • 相关阅读:
    基于uniapp与uview做一个按拼音首字母排序的通讯录页面
    Linux远程工具专家推荐(二)
    ArcMap手动新建矢量要素的方式
    jvs-智能bi(自助式数据分析)9.1更新内容
    跟我读CVPR 2022论文:基于场景文字知识挖掘的细粒度图像识别算法
    独孤思维:不要计较眼前得失,因为你会失去更多
    vscode字符多行自动增长插件。
    【C++】【Opencv】cv::warpAffine()仿射变换函数详解,实现平移、缩放和旋转等功能
    nginx部署vue项目(包括一个nginx部署多个vue项目)
    mvvm框架下对wpf的DataGrid多选,右键操作
  • 原文地址:https://blog.csdn.net/qq_62309585/article/details/126794325