深入探究boost之timer库(1)

1.timer

class timer
{
private:
    std::clock_t _start_time;

public:
//std::clock():自进程启动以来的clock数
//每秒的clock数由 CLOCKS_PER_SEC
    timer(){_start_time=std::clock();}
    void restart() {this->_start_timer=std::clock();}
    double  elapsed() const
    {return douple(std::clock()-this->_start_time)/CLOCKS_PER_SEC;}
    duoble  elapsed_min() const
    {return double(1)/double(CLOCKS_PER_SEC);}
    duoble  elapsed_max() const
    {return (douple((std::numeroc_limits<std::clock_t>::max)())
    -double(_start_time))/double(CLOCKS_PER_SEC);}
   
    ~timer();
};

分析

(1)基础概念:

std::clock 自一个进程开始计时,每秒数由一个CLOCK_PER_SEC 定义

std::numeric_limits 标准库的数值极限类 可以获取最大clock_t值

 (2)

void restart():重新计时

douple elaped ()流逝的时间

(3)简单应用:

#include<boost/timer.hpp>
#include<iostream>
using namespace boost;
int main()
{
    boost::timer t;
    std::cout<<"max timespan:"
        <<t.elapsed_max()/3600<<" h"<<std::endl;
    std::cout<<"min  timespan:"
        <<t.elapsed_min()<<" s"<<std::endl;
    std::cout<<"now time elapsed:"
             <<t.elapsed()<<std::endl;
    
    
}
//结果:
max timespan:2.56205e+09 h
min  timespan:1e-06 s
now time elapsed:0

(4)使用建议:

适用于大部分要求不高的程序计时系统

2.progress_timer

顾名思义:继承timer的全部能力,担有简单的用法 只要声明对象就行啦

例子:

#include<boost/progress.hpp>
int main()
{
   boost::progress_timer t;//声明对象开始计时
   //退出作用域 progress_timer 

}

{
progress_timer t;//第一个计时
//do something
}
{
progress_timer t;//第二个计时
//do something
}

2.1类摘要

progress_timer的摘要:

class progress_timer:public timer,noncopyable
{
 public:
 explicit progress_timer();
 prgress_timer(std::ostream& os);
 ~progress_timer();
};

唯一需要注意的是构造函数,它允许析构时的输出定向到指定的I/O流,默认是std::cout

例子:

  1 #include<boost/progress.hpp>
  2 #include<string>
  3 #include<iostream>
  4 #include<sstream>
  5 int main()
  6 {
  7     std::stringstream ss;
  8     {
  9         boost::progress_timer t(ss);
 10 
 11     }                                                                     
 12     std::cout<<ss.str();
 13  }
 14 

3 progress_display

目的:可以在控制台上显示程序执行进度

3.1类摘要

class progress_dispaly:boost noncopyable
{
    public:
    progress_dispaly(unsigned long expected_count);
    progress_dispaly(unsigned long expected_count,
                     std::ostream& os,
                     const std::string & s1="\n",
                     const std::string&  s2="",
                     const std::string&  s3="");
    void  restart(unsigned long expected_count);
    unsigned long operator+=(unsigned long increment);
    unsigned long operator++();

    unsigned long count() const;
    unsigned long expected_count() const;
};

progress_display 的构造函数接受一个long型的参数expected_count,表示用于进度显示的基数,是最常用的创建progress_display的方法。

效果:

 3.2例子

1 #include <iostream>
  2 #include<boost/progress.hpp>
  3 #include<string>
  4 #include<vector>
  5 #include<string>
  6 #include<fstream>
  7 int main()
  8 {
  9     std::vector<std::string>v(100,"aaa");
 10     v[10]="";
 11     v[23]="";
 12     std::ofstream fs("./test.txt");
 13 
 14     boost::progress_display pd(v.size());
 15 
 16     for(auto pos=v.begin();pos!=v.end();++pos)
 17     {
 18         fs<<*pos<<std::endl;
 19         ++pd;
 20         if(pos->empty())
 21         {
 22         std::cout<<"null string #"
 23             <<(pos-v.begin())<<std::endl;                                 
 24         }
 25     }
 26 }

效果:

 这个显示混乱的问题很难解决,因为我们无法预知庞大的程序之中哪个地方会存在一个可能会干扰progress_display的输出。一个可能(但远非完美〉的办法是在每次显示进度时都调用restart()重新显示进度刻度,然后用operator+=来指定当前进度,而不是简单地调用operator++,例如:

pd.restart(v.size())

pd+=(pos-v.begin()+1)

 关注我一起学习

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值