Boost之时间处理(timer库)

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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值