首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >boost之timer

boost之timer

作者头像
全栈程序员站长
发布2022-07-18 15:04:47
发布2022-07-18 15:04:47
1.3K0
举报

大家好,又见面了,我是全栈君

1. timer类实现

代码语言:javascript
复制
#pragma once

#include <ctime>
#include <limits>  

class timer
{
public:
    timer(){ _start_time = clock(); }
    void restart(){ _start_time = clock(); }
    double elapsed() const
    {
        return (double)(clock() - _start_time) / CLOCKS_PER_SEC; 
    }
    double elapsed_min() const
    {
        return (double)(1) / (double)CLOCKS_PER_SEC;
    }
    double elapsed_max() const
    {
        return (double)(std::numeric_limits<clock_t>::max() - _start_time) / double(CLOCKS_PER_SEC);
        
    }
private:
    clock_t _start_time;
};

2. 重点说明

2.1 CLOCKS_PER_SEC

  timer的计数使用了标准头文件<ctime>里的clock()函数,它返回自进程启动以来的clock计数,每秒的clock数由宏CLOCKS_PER_SEC定义,CLOCKS_PER_SEC的值因操作系统而不同,在win32下是1000,而在linux下则是1000000,页就是说在win32下的精度是毫秒,在linux下的精度是微妙。

2.2 numeric_limits模版

  说白了,它是一个模板类,它主要是把C++当中的一些内建型别进行了封装,比如说numeric_limits<int>是一个特化后的类,从这个类的成员变量与成员函数中,我们可以了解到int的很多特性:可以表示的最大值,最小值,是否是精确的,是否是有符号等等。如果用其他任意(非内建类型)来特化这个模板类,比如string,string怎么可能有最大值?我们从MSDN上可以了解到,这对string,成员变量与成员函数是没有意义的,要么返回0要么为false。

  参考博客:http://blog.163.com/wujiaxing009%40126/blog/static/7198839920124135147911/

2.3 使用建议

  timer不适合高精度的时间测量任务,它的精度依赖操作系统或编译器,难以做到跨平台,timer也不适合大跨度时间段的测量,可提供的最大时间跨度只有几百个小时,如果需要以天、月甚至年作为时间的单位则不能使用timer,应使用date_time.

3. 扩展new_progress_timer

3.1 代码实现

代码语言:javascript
复制
template<int N = 2>
class new_progress_timer : public timer
{
public:
    new_progress_timer(ostream &os = cout)
        :m_os(os)
    {

    }

    ~new_progress_timer()
    {
        try
        {
            // 保存流的状态
            ostream::fmtflags old_flags = m_os.setf(ostream::fixed, ostream::floatfield);
            streamsize old_prec = m_os.precision(N);

            // 输出时间
            m_os << elapsed() << "s\n" << endl;

            // 恢复流状态
            m_os.flags(old_flags);
            m_os.precision(old_prec);
        }
        catch (...){}
    }
private:
    ostream &m_os;   // 需要特别注意
};

继承于timer类,主要实现输出时间的精度控制

注意代码的最后一行,原因是:

代码语言:javascript
复制
protected:
    __CLR_OR_THIS_CALL basic_ostream(_Myt&& _Right)
        {    // construct by moving _Right
        _Myios::init();
        _Myios::move(_STD move(_Right));
        }

    _Myt& __CLR_OR_THIS_CALL operator=(_Myt&& _Right)
        {    // move from _Right
        this->swap(_Right);
        return (*this);
        }

    void __CLR_OR_THIS_CALL swap(_Myt& _Right)
        {    // swap with _Right
        if (this != &_Right)
            _Myios::swap(_Right);
        }

public:
    __CLR_OR_THIS_CALL basic_ostream(const _Myt&) = delete;
    _Myt& __CLR_OR_THIS_CALL operator=(const _Myt&) = delete;

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/120120.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年12月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. timer类实现
  • 2. 重点说明
    • 2.1 CLOCKS_PER_SEC
    • 2.2 numeric_limits模版
    • 2.3 使用建议
  • 3. 扩展new_progress_timer
    • 3.1 代码实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档