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

 

你们的评论、反馈,及对你们有所用,是我整理材料和博文写作的最大的鼓励和唯一动力。欢迎讨论和关注!
没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。