boost之时间timer
C++一直缺乏对时间和日期的处理能力,一般借助于C的struct tm和time();timer包含三个类其中timer,progress_timer是计时器类,进度指示类是progress_display.
1.timer类,内部封装的是std::clock,精度依赖于具体的编译器和操作系统。只能计算从声明对象到对象结束的时间。
#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;
}
2.progress_timer继承与timer但是不需要显示调用elapsed当对象退出作用域时会自动调用。
#include <iostream>
#include <boost/progress.hpp>
#include <boost/thread.hpp>
using namespace std;
using namespace boost;
int main()
{
progress_timer t;
this_thread::sleep(posix_time::seconds(2));
return 0;
}
3.progress_display类用于在标准输出显示进度
#include <iostream>
#include <fstream>
#include <vector>
#include <boost/progress.hpp>
using namespace std;
using namespace boost;
int main()
{
vector<string> vec(100000);
fstream fs("c:\\test.txt");
progress_display pd(vec.size());
for (vector<string>::iterator pos = vec.begin();pos != vec.end();++pos)
{
fs << *pos <<endl;
++pd;
}
return 0;
}
一切源于对计算机的热爱

浙公网安备 33010602011771号