timer库包含三个小组件,分别是计时器timer、progress_timer和进度
一. timer
timer是一个小型的计时器,提供毫秒级别的计时精度和操作函数
#include <iostream>
#include <boost/timer.hpp>
using namespace std;
using namespace boost;
int main()
{
timer t;
cout << "max timespan:" << t.elapsed_max() /3600 << "h" <<endl; //可度量的最大时间
cout << "min timespan:" << t.elapsed_min() << "s" << endl; //可度量的最小时间
cout << "now time elapsed:" << t.elapsed() <<"s" << endl; //输出已经流逝的时间
return 0;
}
二. progress_timer
progress_timer继承自timer,会在析构时自动输出时间,省去了timer手动调用elapsed()的工作。
#include <iostream>
#include <sstream>
#include <boost/progress.hpp>
using namespace std;
using namespace boost;
int main()
{
int i = 100000000;
{
boost::progress_timer t;
while (i)
--i;
} //progress_timer会在这里析构,自动输出时间
i = 10000000;
stringstream ss;
{
progress_timer t(ss); //progress_timer可以将时间转移到指定的IO流中
while (i)
--i;
}
cout << ss.str();
return 0;
}
三. progress_display
progress_display是一个独立的类,在构造后显示出一个类似图形化进度条的界面,用两行以字符的形式显示百分比进度。然而要注意的是,如果程序本身执行的操作也是向标准输出输出字符,那么progress_display的进度显示将会一片混乱。
#include <iostream>
#include <fstream>
#include <vector>
#include <boost/progress.hpp>
using namespace std;
using namespace boost;
int main()
{
vector<string> v(1000000);
ofstream fs("./test.txt");
progress_display pd(v.size());
for (auto& x : v)
{
fs << x << endl;
++pd; //更新进度显示
}
return 0;
}