C++大法:举世闻名之BOOST大法精华浅析(二)timer(小白piao分享)

二、boost下的timer

2.1 timer库的用途:

​ 性能测试时候的计时任务。精确到毫秒级别,就是一个方便的秒表。

​ timer库的三个组件

  • timer
  • prograss_timer
  • prograss_display : 进度指示类

2.2 timer简单例子:

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

using namespace std;
using namespace boost;//注意包含boost标准命名空间

int main()
{
  timer t; //创建计时器对象
  cout<<t.elapsed_max()/3600<<"h"<<endl;//elapsed_max()返回计时器最大值,单位秒。

  cout<<t.elapsed_min()<<"s"<<endl;//最小计时单位

  //输出已经流失的时间:
  cout<<t.elapsed()<<endl;
  return 0;
}

2.3 timer的源码:(.hpp)

​ 首先,boost库最大的特点就是.hpp文件,其文件特点是将声明和实现都在此文件中,不像c++STL库那样分.cpp和.h,即hpp == cpp + h。

class timer
{
 public:
         timer() { _start_time = std::clock(); } // postcondition: elapsed()==0
//         timer( const timer& src );      // post: elapsed()==src.elapsed()
//        ~timer(){}
//  timer& operator=( const timer& src );  // post: elapsed()==src.elapsed()
  void   restart() { _start_time = std::clock(); } // post: elapsed()==0
  double elapsed() const                  // return elapsed time in seconds
    { return  double(std::clock() - _start_time) / CLOCKS_PER_SEC; }

  double elapsed_max() const   // return estimated maximum value for elapsed()
  // Portability warning: elapsed_max() may return too high a value on systems
  // where std::clock_t overflows or resets at surprising values.
  {
    return (double((std::numeric_limits<std::clock_t>::max)())
       - double(_start_time)) / double(CLOCKS_PER_SEC); 
  }

  double elapsed_min() const            // return minimum value for elapsed()
   { return double(1)/double(CLOCKS_PER_SEC); }

 private:
  std::clock_t _start_time;
}; // timer

2.4 timer的使用建议:

​ 最大是500多个小时(长时间计时使用date_time),不适合高精度。不能适应于跨平台(win32上是毫秒最小,linux上是微秒)。

2.5 prograss_timer

​ 是一个继承自timer的小工具,在析构时自动打印耗时,可以省略timer中的elapsed()函数。prograss_timer在boost命名空间中,为了使用prograss_timer组件,需要包含头文件<boost/prograss_timer.hpp>。

2.6 prograss_timer简单例子:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kDkXY1yr-1608099743380)(https://i.loli.net/2020/12/04/vMB4zAjwa9Cs3xZ.png)]

2.7 prograss_timer源码:

#ifndef BOOST_PROGRESS_HPP
#define BOOST_PROGRESS_HPP

#include <boost/config/header_deprecated.hpp>
BOOST_HEADER_DEPRECATED( "the facilities in <boost/timer/timer.hpp> or <boost/timer/progress_display.hpp>" )

#include <boost/timer.hpp>
#include <boost/noncopyable.hpp>
#include <boost/cstdint.hpp>  // for uintmax_t
#include <iostream>           // for ostream, cout, etc
#include <string>             // for string

namespace boost {

//  progress_timer  ----------------------------------------------------------//

//  A progress_timer behaves like a timer except that the destructor displays
//  an elapsed time message at an appropriate place in an appropriate form.

class progress_timer : public timer, private noncopyable
{
  
 public:
  explicit progress_timer( std::ostream & os = std::cout )
     // os is hint; implementation may ignore, particularly in embedded systems
     : timer(), noncopyable(), m_os(os) {}
  ~progress_timer()
  {
  //  A) Throwing an exception from a destructor is a Bad Thing.
  //  B) The progress_timer destructor does output which may throw.
  //  C) A progress_timer is usually not critical to the application.
  //  Therefore, wrap the I/O in a try block, catch and ignore all exceptions.
    try
    {
      // use istream instead of ios_base to workaround GNU problem (Greg Chicares)
      std::istream::fmtflags old_flags = m_os.setf( std::istream::fixed,
                                                   std::istream::floatfield );
      std::streamsize old_prec = m_os.precision( 2 );
      m_os << elapsed() << " s\n" // "s" is System International d'Unites std
                        << std::endl;
      m_os.flags( old_flags );
      m_os.precision( old_prec );
    }

    catch (...) {} // eat any exceptions
  } // ~progress_timer

 private:
  std::ostream & m_os;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白piao

创作不易,支持一下!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值