深入探究boost之timer库(1)

追求适度,才能走向成功;人在顶峰,迈步就是下坡;身在低谷,抬足既是登高;弦,绷得太紧会断;人,思虑过度会疯;水至清无鱼,人至真无友,山至高无树;适度,不是中庸,而是一种明智的生活态度。

导读:本篇文章讲解 深入探究boost之timer库(1),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

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的方法。

效果:

深入探究boost之timer库(1)

 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 }

效果:

深入探究boost之timer库(1)

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

pd.restart(v.size())

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

深入探究boost之timer库(1)

 关注我一起学习

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/129679.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!