- class timer
- {
- private:
- std::clock_t _start_time;
-
- public:
- //std::clock():自进程启动以来的clock数
- //每秒的clock数由 CLOCKS_PER_SEC
- timer(){_start_time=std::clock();}
- void restart() {this->_start_timer=std::clock();}
- double elapsed() const
- {return douple(std::clock()-this->_start_time)/CLOCKS_PER_SEC;}
- duoble elapsed_min() const
- {return double(1)/double(CLOCKS_PER_SEC);}
- duoble elapsed_max() const
- {return (douple((std::numeroc_limits
clock_t>::max)()) - -double(_start_time))/double(CLOCKS_PER_SEC);}
-
- ~timer();
- };
-
分析
(1)基础概念:
std::clock 自一个进程开始计时,每秒数由一个CLOCK_PER_SEC 定义
std::numeric_limits 标准库的数值极限类 可以获取最大clock_t值
(2)
void restart():重新计时
douple elaped ()流逝的时间
(3)简单应用:
- #include
- #include
- using namespace boost;
- int main()
- {
- boost::timer t;
- std::cout<<"max timespan:"
- <
elapsed_max()/3600<<" h"< - std::cout<<"min timespan:"
- <
elapsed_min()<<" s"< - std::cout<<"now time elapsed:"
- <
elapsed()< -
-
- }
- //结果:
- max timespan:2.56205e+09 h
- min timespan:1e-06 s
- now time elapsed:0
(4)使用建议:
适用于大部分要求不高的程序计时系统
2.progress_timer
顾名思义:继承timer的全部能力,担有简单的用法 只要声明对象就行啦
例子:
- #include
- int main()
- {
- boost::progress_timer t;//声明对象开始计时
- //退出作用域 progress_timer
-
- }
-
- {
- progress_timer t;//第一个计时
- //do something
- }
- {
- progress_timer t;//第二个计时
- //do something
- }
2.1类摘要
progress_timer的摘要:
- class progress_timer:public timer,noncopyable
- {
- public:
- explicit progress_timer();
- prgress_timer(std::ostream& os);
- ~progress_timer();
- };
唯一需要注意的是构造函数,它允许析构时的输出定向到指定的I/O流,默认是std::cout
例子:
- 1 #include
- 2 #include
- 3 #include
- 4 #include
- 5 int main()
- 6 {
- 7 std::stringstream ss;
- 8 {
- 9 boost::progress_timer t(ss);
- 10
- 11 }
- 12 std::cout<
str(); - 13 }
- 14
3 progress_display
目的:可以在控制台上显示程序执行进度
3.1类摘要
- class progress_dispaly:boost noncopyable
- {
- public:
- progress_dispaly(unsigned long expected_count);
- progress_dispaly(unsigned long expected_count,
- std::ostream& os,
- const std::string & s1="\n",
- const std::string& s2="",
- const std::string& s3="");
- void restart(unsigned long expected_count);
- unsigned long operator+=(unsigned long increment);
- unsigned long operator++();
-
- unsigned long count() const;
- unsigned long expected_count() const;
- };
progress_display 的构造函数接受一个long型的参数expected_count,表示用于进度显示的基数,是最常用的创建progress_display的方法。
效果:

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