微秒级时间函数

#ifndef __CPerfCounter__
    #define __CPerfCounter__

#include <boost/chrono/chrono_io.hpp>
#include <string>
#include <sstream>
#include<string.h>

#include "boost/predef/platform.h"

class CPerfCounter
{
    #define DEF_MAX_PERF_ARRAY  64
private:
    boost::chrono::high_resolution_clock::time_point m_time_prev;
    int64_t m_arPerf[DEF_MAX_PERF_ARRAY]; //性能数组
    int m_nSize;
    std::string m_strPerfName;

	int m_counter;
protected:
    void ClearRecord()
    {
        m_strPerfName.clear();
        memset(m_arPerf, 0, sizeof(m_arPerf));
        m_nSize = 0;
		m_counter = 0;
    }
    //-----------------------------------------------------------------------------
public:
    CPerfCounter()
    {
        ClearRecord();
        GetElapse();
    }
    //-----------------------------------------------------------------------------
    int64_t GetTickCountUS()
    {//得到递增的tickus
        boost::chrono::high_resolution_clock::time_point now       = boost::chrono::high_resolution_clock::now();
        boost::chrono::microseconds us = boost::chrono::duration_cast<boost::chrono::microseconds> (now - m_time_prev);
        return us.count();
    }
    //-----------------------------------------------------------------------------
    int64_t GetElapse()
    {//得到上次时间与这次的差, us
        boost::chrono::high_resolution_clock::time_point now       = boost::chrono::high_resolution_clock::now();
        boost::chrono::microseconds us = boost::chrono::duration_cast<boost::chrono::microseconds> (now - m_time_prev);
        m_time_prev = now;
        return us.count();
    }
    //-----------------------------------------------------------------------------
    int Record(std::string strPerfName="")
    {//记录调用时间到数组
        if (strPerfName.length()!=0)
        {
            ClearRecord();
            m_strPerfName = strPerfName;
        }

        m_arPerf[m_nSize%DEF_MAX_PERF_ARRAY] = GetElapse();
        m_nSize++;

        return m_nSize;
    }
    //-----------------------------------------------------------------------------
    std::string GetRecord()
    {
        std::stringstream stream;
        int64_t int64Total = 0;

        stream << "---------------------------------------" << std::endl;
        stream << "PrefName:" << m_strPerfName << std::endl;
        for (int i=0; i<m_nSize; i++)
        {
            stream <<  "Step" << i+1 << ".ElapseTime:" << m_arPerf[i] << "us" << std::endl;
            int64Total +=  m_arPerf[i];
        }
        stream  << "TotalElapseTimes:" << int64Total << "us" << std::endl;

        return stream.str();
    }
    //-----------------------------------------------------------------------------
    int GetTPS(int value)
    {//得到调用次数,秒为单位
		if (m_counter==0)
		{
			m_counter = value;
			return 0;
		}

		int64_t count   = (int64_t)(value - m_counter);
		m_counter = value;

		int time_us = (int)GetElapse();

		return (int)((1000000*count)/time_us);
    }

	int64_t GetValueByIndex(int index)
    {
		if(index >= m_nSize)
			return 0;
        return m_arPerf[index];
    }
};
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值