Boost库学习笔记 2.1 Boost时间与日期timer库

4 篇文章 0 订阅
2 篇文章 0 订阅
Boost库学习笔记 2.1 Boost时间与日期timer库

1. timer库介绍
    timer库是一个很小库,提供简易度量时间和进度显示功能,可以用于性能测试等需要计时的任务。
    timer库包含三个组件,分别是timer、progress_timer、 progress_display.

2. timer类
    timer类可以测量时间的流逝,是一个小型的计时器,提供毫秒级别的计时精度和操作函数。

    timer类引用头文件:
    #include <boost/timer.hpp>
    using namespace boost;

    timer类公共成员函数:

    class timer{
    ......
    public:
        timer();            // 构造函数,计时开始
        void restart();            // 计时重新开始
        double elapsed();        // 已经流逝的时间(秒)
        double elapsed_min();        // 可度量的最小时间(秒),Win32平台精确度最小能够达到毫秒,Linux精确到微秒。
        double elapsed_max();        // 可度量的最大时间(秒)
    ......
    };



    timer类使用示列代码:
  
 /* Boost时间与日期timer库  timer类使用示列代码
     * Lixiujie www.xiujie.cn
     * 2011-11-01
     */
    #include <string>
    #include <iostream>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    using namespace std;

    #include <boost/timer.hpp>
    using namespace boost;

    int main(int argc, char *argv[]){
        timer t;
        // timer 能记录的最长时间
        cout << "max timespan:" << t.elapsed_max()/3600 << "h" << endl;
        // timer 能记录的最短时间
        cout << "min timespan:" << t.elapsed_min() << "s" << endl;
        // 流逝时间
        cout << "now time elapsed:" << t.elapsed() << "s" << endl;
        long i = 0, y = 0;
        // 重新开始
        t.restart();
        for (i=0;i<2000000000;i++){
            y++;
        }
        cout << "for i++ time elapsed:" << t.elapsed() << "s" << endl;
        y = 0;
        t.restart();
        for (i=0;i<2000000000;++i){
            ++y;
        }
        cout << "for ++i time elapsed:" << t.elapsed() << "s" << endl;
        return 0;
    }



    timer类使用示列代码屏幕输出:
    max timespan:596.523h
    min timespan:0.001s
    now time elapsed:0s
    for i++ time elapsed:5.937s
    for ++i time elapsed:5.953s

3. progress_timer类
    progress_timer继承timer的全部能力, 析构时自动输出流逝的时间。

    progress_timer类引用头文件:
    #include <boost/progress.hpp>
    using namespace boost;

    progress_timer类公共成员函数:
   	progress_timer: public timer, ......{
	......
	public:
		progress_timer(std::ostream & os = std::cout);	// 构造,默认输出流为屏幕
		~progress_timer();				// 析构,自动输出流逝时间(秒),精度到小数后两位。
	......
	};



    progress_timer类使用示列代码:
    /* Boost时间与日期timer库  progress_timer类使用示列代码
     * Lixiujie www.xiujie.cn
     * 2011-11-01
     */
    #include <string>
    #include <iostream>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    using namespace std;
    #include <boost/progress.hpp>
    using namespace boost;

    int main(int argc, char *argv[]){
        progress_timer t;
        return 0;
    }



    progress_timer类使用示列代码屏幕输出:
    0.00 s

4. progress_display类
    progress_display类是一个独立的类与timer库上其它两个组件没有联系。主要功能产生一个进度条界面。
    不好用,容易被其它屏幕输出打断,不做过多的介绍。

    使用示列代码:
 
   /* Boost时间与日期timer库  progress_display类使用示列代码
     * Lixiujie www.xiujie.cn
     * 2011-11-01
     */
    #include <string>
    #include <iostream>
    #include <vector>
    #include <set>
    #include <map>
    #include <algorithm>
    using namespace std;

    #include <boost/progress.hpp>
    using namespace boost;

    int main(int argc, char *argv[]){
        progress_display pd(100);
        int i = 0;
        for(i=0;i<100;++i){
            ++pd;
        }
        return 0;
    }


    示列代码屏幕输出:
    
    0%   10   20   30   40   50   60   70   80   90   100%
    |----|----|----|----|----|----|----|----|----|----|
    ***************************************************


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值