boost中使用 timer

REF:boost库使用—计时器类timer, 19.12

timer是一个很小的库,提供简单的时间度量和进度显示功能,也可用于性能测试等计时任务。timer库包含三个组件:计时器类timer、progress_timer和进度指示类progress_display。

计时器类timer

需包含头文件 #include <boost/timer.hpp>

示例

#include <iostream>
#include <windows.h>
#include <boost/timer.hpp>

using namespace std;

int main()
{
    //开始计时
    boost::timer t;
    //可度量最大时间
    cout << "max timespan:" << t.elapsed_max() / 3600 << "h" << endl;
    //可度量最小时间
    cout << "min timespac:" << t.elapsed_min() << endl;
    //第一次计时
    cout << "now time elapsed:" << t.elapsed() << endl;
    Sleep(1000);
    //第二次计时
    cout << "now time elapsed:" << t.elapsed() << endl;
    return 0;
}

计时器类progress_timer

包含头文件:

#include <boost/progress.hpp>

说明:

progress_timer继承于timer,所以timer的函数也可以调用;

progress_timer计时是析构后自动输出;也可以多个计时,需要用{}包含

示例

#include <iostream>
#include <windows.h>
#include <boost/progress.hpp>

using namespace std;

int main()
{
    {
        //多个计时
        {
            //第一个计时
            //启动计时器
            boost::progress_timer tt;
            Sleep(100);
            cout << "one progress time elapsed:" << tt.elapsed() << endl;
        }
        //第二个计时
        {
            boost::progress_timer tt;
            Sleep(100);
            //输出计时器数
            cout << "two progress time elapsed:" << tt.elapsed() << endl;
            //{}结束代表析构函数
        }
    }
    system("pause");
    return 0;
}

进度指示类progress_display

包含头文件:

#include <boost/progress.hpp>

不太合理,不建议使用。

示例

#include <iostream>
#include <windows.h>
#include <boost/timer.hpp>
#include <vector>
#include <boost/progress.hpp>


using namespace std;
vector<string>v(100);
    //申明基数大小
    boost::progress_display pd(v.size());
    for (auto it = v.begin(); it != v.end(); it++)
    {
        Sleep(100);
        //正常使用,没有其他输出
        //++pd;
        cout << "hello progress_display" << endl;
        //输出会打乱格式
        pd.restart(v.size());
        pd += (it-v.begin()+1);
    }
    system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/arxive/p/13381282.html