时间和日期

Boost使用的timer和data_timerj进行对应和时间日期相关的出来文档,timer包含三个组件,分别为timer,progress_timer以及对应的progress_display.

timer

timer可以测量运行时间

timer类摘要

lass 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

            } // namespace boost

demo1

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

using namespace boost;
int main(int argc, char const *argv[])
{
    timer t;
    std::cout << "max timespan:" << t.elapsed_max() << std::endl;
    std::cout << "min timespan:" << t.elapsed_min() << std::endl;
    std::cout <<"now time elapsed:" << t.elapsed()<< "s" <<std::endl;


    return 0;
}

输出的结果

$ ./a.out 
max timespan:9.22337e+12
min timespan:1e-06
now time elapsed:0.000256s

progress_timer

progress_timer测量经过的时间(使用计时器),销毁时显示。其继承于timer

progress_timer类摘要

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

demo2例子

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

void cost_timer(){
    boost::progress_timer t;
    int i = 0;
    while (i < 10000000)
    {
        i++;
    }
    return;
    
}
int main(int argc, char const *argv[])
{
    cost_timer();

    return 0;
}

输出结果

 ./boost_timer_demo2 
0.05 s

progress_display

显示已知目标的进度指示。

progress_display类摘要

class progress_display : private noncopyable
{
 public:
  explicit progress_display( unsigned long expected_count_,
                             std::ostream & os = std::cout,
                             const std::string & s1 = "\n", //leading strings
                             const std::string & s2 = "",
                             const std::string & s3 = "" )
   // os is hint; implementation may ignore, particularly in embedded systems
   : noncopyable(), m_os(os), m_s1(s1), m_s2(s2), m_s3(s3) { restart(expected_count_); }

  void           restart( unsigned long expected_count_ )
  //  Effects: display appropriate scale
  //  Postconditions: count()==0, expected_count()==expected_count_
  {
    _count = _next_tic_count = _tic = 0;
    _expected_count = expected_count_;

    m_os << m_s1 << "0%   10   20   30   40   50   60   70   80   90   100%\n"
         << m_s2 << "|----|----|----|----|----|----|----|----|----|----|"
         << std::endl  // endl implies flush, which ensures display
         << m_s3;
    if ( !_expected_count ) _expected_count = 1;  // prevent divide by zero
  } // restart

  unsigned long  operator+=( unsigned long increment )
  //  Effects: Display appropriate progress tic if needed.
  //  Postconditions: count()== original count() + increment
  //  Returns: count().
  {
    if ( (_count += increment) >= _next_tic_count ) { display_tic(); }
    return _count;
  }

  unsigned long  operator++()           { return operator+=( 1 ); }
  unsigned long  count() const          { return _count; }
  unsigned long  expected_count() const { return _expected_count; }

  private:
  std::ostream &     m_os;  // may not be present in all imps
  const std::string  m_s1;  // string is more general, safer than 
  const std::string  m_s2;  //  const char *, and efficiency or size are
  const std::string  m_s3;  //  not issues

  unsigned long _count, _expected_count, _next_tic_count;
  unsigned int  _tic;
  void display_tic()
  {
    // use of floating point ensures that both large and small counts
    // work correctly.  static_cast<>() is also used several places
    // to suppress spurious compiler warnings. 
    unsigned int tics_needed = static_cast<unsigned int>((static_cast<double>(_count)
        / static_cast<double>(_expected_count)) * 50.0);
    do { m_os << '*' << std::flush; } while ( ++_tic < tics_needed );
    _next_tic_count = 
      static_cast<unsigned long>((_tic/50.0) * static_cast<double>(_expected_count));
    if ( _count == _expected_count ) {
      if ( _tic < 51 ) m_os << '*';
      m_os << std::endl;
      }
  } // display_tic
};

demo使用例子

#include <boost/progress.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;
using namespace boost;

int main(int argc, char const *argv[])
{
    
    vector<string> v(100);
    for(int i = 0; i < 100 ; i++){
        v[i] = i;
    }
    ofstream fs("demo.txt");
    progress_display pd(v.size());
    vector<string>::iterator pos;
    for (pos = v.begin(); pos < v.end(); pos++)
    {
        fs << *pos;
        ++pd;
    }
    
    return 0;
}

输出的结果为:

$ ./boost_timer_demo3

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、付费专栏及课程。

余额充值