boost 学习笔记 2: timer

boost 学习笔记 2: timer 

copy from:http://einverne.github.io/post/2015/12/boost-learning-note-2.html

1:timer

  头文件

  timer 位于boost命名空间,需要包含头文件 <boost/timer.hpp> 即:

#include <boost/timer.hpp>
using namespace boost;

基本方法

timer变量声明之后即开始计时工作,之后可以调用 elapsed() 方法来测量对象自创建之后所流逝的时间。成员函数 elapsed_max() 和 elapsed_min() 方法返回 timer 能够测量的时间最大值和最小值,输出单位秒。timer类的实现代码很少,可参考源码学习。

timer 实现源码

使用标准库头文件 中的 std::clock() 函数。

使用建议

  • 不适合高精度计时,精度依赖操作系统或编译器
  • 不适合大跨度时间段测量,最大跨度只有几百小时,如需要天、月甚至年做时间单位,可使用 date_time 库
 

2:progress_timer

头文件

#include <boost/progress.hpp>
using namespace boost;

基本用法

在定义 progress_timer 之后,析构对象时会自动输出流逝时间。可以使用花括号,来使 progress_timer 实现计时功能。

progress_timer t;
// do some work cout << t.elapsed() << endl; //print out elapsed time for the task

progress_timer 精度问题,只能保留到小数点后两位,精确到百分之一秒。

3:progress_display

头文件

#include <boost/progress.hpp>
using namespace boost;

基本用法

在构造函数中传入总数,然后迭代更新进度条。

vector<string> v(100); ofstream fs("test.txt"); progress_display pd(v.size()); // 构造函数,传入进度总数 vector<string>::iterator pos; for (pos = v.begin(); pos != v.end(); ++pos){ // 遍历迭代,处理字符串,写文件 fs << *pos << endl; ++pd; // 更新进度 }

progress_display 问题

progress_display 向标准输出 cout 输出字符,无法将进度条和输出分离,如果循环中带有输出,则显示会很难看。

 

 1 #include <boost/timer.hpp>
 2 #include <boost/progress.hpp>
 3 #include <iostream>
 4 #include <stdio.h>
 5 using namespace boost;
 6 using namespace std;
 7 
 8 int main()
 9 {
10         timer t;
11         cout<<"max timespan:"<<t.elapsed_max()/3600<<"h"<<endl;
12         cout<<"min timespan:"<<t.elapsed_min()<<"s"<<endl;
13         cout<<"now time elapsed:"<<t.elapsed()<<"s"<<endl;
14 
15         //progress_timer
16         progress_timer t1;
17         cout<<t1.elapsed()<<endl;
18 
19         char c= getchar();
20         return 0;
21 }

猜你喜欢

转载自www.cnblogs.com/zhaohu/p/9105009.html