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