#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
微秒级时间函数
最新推荐文章于 2022-09-07 21:00:00 发布