C++Boost库学习之timer库(二)

目录

1.timer库概述
  ①命名空间timer
  ②计时器类中用到的格式
2.类cpu_timer
  ①类定义
  ②使用例子
3.类auto_cpu_timer
  ①类定义
  ②使用例子

1.timer库概述 ^

  timer是一个很小的库,提供简易的计时功能,对了解程序执行所需的时间在测试和生产环境中都很有用。

  旧版本的计时器已经被弃用了,取而代之的是是更符合当前boost实践的CPU计时器cpu_timer和auto_cpu_timer。所有组件都在命名空间timer中,如下:

 ①命名空间timer ^
//头文件:<boost/timer/timer.hpp>
namespace boost
{
  namespace timer
  {
    class cpu_timer;       
    class auto_cpu_timer;  

    //纳秒级别的类型
    typedef boost::int_least64_t nanosecond_type;
    //结构体,提供纳秒级别的经过时间,用户时间,系统时间
    struct cpu_times
    {
      nanosecond_type wall;
      nanosecond_type user;
      nanosecond_type system;

      void clear();
    };
    //默认精度为6
    const int default_places = 6;

    //将时间转化为指定精度,或指定精度与格式的字符串,单位为秒
    std::string format(const cpu_times& times, short places, const std::string& format); 
    std::string format(const cpu_times& times, short places = default_places); 
  }
}
 ②计时器类中用到的格式 ^
格式字符串 形式
%w times.wall
%u times.user
%s times.system
%t times.user + times.system
%p times.wall,times.user + times.system

2.类cpu_timer ^

  cpu_timer对象测量经过时间,用户进程时间和系统处理时间。

  • 经过时间:开始计时与结束计时所花时间
  • 用户进程时间:用户调用进程的指令而收取的CPU时间
  • 系统响应时间:系统响应用户请求的CPU时间
  •  ①cpu_timer类定义 ^
    class cpu_timer
    {
    public:
        //关键字noexcept声明函数不会抛出异常
        //=default用于告诉编译器生成默认版本
        cpu_timer() noexcept;
        ~cpu_timer() noexcept = default; 
        cpu_timer(const cpu_timer&) noexcept = default;
        cpu_timer& operator=(const cpu_timer&) noexcept = default;      
    
        //如果对象调用了stop()函数,则返回true,否则返回false
        bool is_stopped() const noexcept; 
    
         //如果is_stopped()为真,返回start()至stop()之间的时间间隔,否则返回start()至调用此函数的时间间隔
        cpu_times elapsed() const noexcept;
        //参数places代表精度,默认为6,即精确到小数点后6位,单位为秒
        //参数format代表时间输出格式,常用的是%w,表示经过时间
        //用于返回时间的字符串
        std::string  format(int places, const std::string& format) const;
        std::string  format(int places = default_places) const;
    
    
        void start() noexcept;   //开始计时
        void stop() noexcept;    //停止计时
        void resume() noexcept;  //如果is_stopped()为真,恢复累计额外的经过时间,截止至当前时间值,否则没效果。
    };

    注:resume()只是接着计时,并不是重新开始计时,如果想重新开始计时应该调用start()

     ②使用例子 ^
    #include<iostream>
    #include <boost/timer/timer.hpp>
    using namespace boost;
    using std::cout;
    using std::endl;
    
    int main()
    {
        timer::cpu_timer t;
        t.start();
        for (int i = 0; i != 3000; ++i)
        {
            cout << i << endl;
        }
        t.stop();
        cout<<t.format();      //以默认6精度输出经过时间,用户时间,系统时间
        //输出为真
        if (t.is_stopped())
            cout << "stop()之后resume()之前:为真" << endl;
        else
            cout << "stop()之后resume()之前:为假" << endl;
        t.resume();
        //输出为假
        if (t.is_stopped())
            cout << "stop()和resume()之后:为真" << endl;
        else
            cout << "stop()和resume()之后:为假" << endl;
        for (int i = 0; i != 200; ++i)
        {
            cout << i << endl;
            if (i == 150)
            {
                //注:resume()的调用会让is_stopped()为假
                cout << t.elapsed().wall << endl;   //输出:start()至调用此函数的经过时间
                cout << t.elapsed().user << endl;   //输出:start()至调用此函数的用户时间
                cout << t.elapsed().system << endl; //输出:start()至调用此函数的系统时间
            }
    
        }
        t.stop();
        cout << t.format(3,"%w");   //以3精度输出经过时间
    }

    3.类auto_cpu_timer ^

      auto_cpu_timer继承至cpu_timer,是一个更为精细的cpu_timer,对象创建后即开始计时,当它被销毁时能自动的报告花费的时间。

     ①auto_cpu_timer类定义 ^
    class auto_cpu_timer : public cpu_timer
    {
    public:
        //explicit关键字是静止构造函数进行隐式转换
        //共六个构造函数,及三个参数,为places(精度),format(格式),ostream(输出流)
        explicit auto_cpu_timer(short places = default_places);
        auto_cpu_timer(short places, const std::string& format);
        explicit auto_cpu_timer(const std::string& format);
        auto_cpu_timer(std::ostream& os, short places, const std::string& format);
        explicit auto_cpu_timer(std::ostream& os, short places = default_places);
        auto_cpu_timer(std::ostream& os, const std::string& format);
        ~auto_cpu_timer() noexcept;
    
        //拷贝构造函数及赋值运算符
        auto_cpu_timer(const auto_cpu_timer&) = default;
        auto_cpu_timer& operator=(const auto_cpu_timer&) = default;
    
        //返回当前对象用到的输出流
        std::ostream&  ostream() const noexcept;
        //返回输出时间的精度
        short  places() const noexcept;
        //返回输出时间的格式字符串
        const std::string& format_string() const noexcept;
    
        //在计时器运行期间调用,输出start()至调用该函数的经过时间,如果在stop()之后调用则无效。
        void report();
    };
    ②使用例子 ^
    #include<iostream>
    #include <boost/timer/timer.hpp>
    using namespace boost;
    using std::cout;
    using std::endl;
    int main()
    {
        //以4精度,%w格式构造一个auto_cpu_timer对象
        timer::auto_cpu_timer t(4,"%w");
        for (int i = 0; i != 3000; ++i)
        {
            cout << i << endl;
        }
        for (int i = 0; i != 200; ++i)
        {
            t.report();     //输出对象创建后至调用该函数的经过时间
            cout << endl;
        }
        cout << "精度:" << t.places() << endl;        //输出:4
        cout << "格式:" << t.format_string() << endl; //输出:%w
        cout << "对象销毁时的经过时间:";                
    }

    猜你喜欢

    转载自blog.csdn.net/qq_17044529/article/details/82551737