c++中精确计时的类
程序员文章站
2024-01-24 09:00:04
...
为了测试系统的效率,写了两个计时的类,备份下。
头文件
#pragma once
#include <windows.h>
class CStaticTime
{
public:
CStaticTime();
~CStaticTime();
public:
void TimingStart();
void TimingEnd();
double GetInterval();
double GetTicksCount();
double GetInterval(const double timeF,const double timeB);
private:
LARGE_INTEGER m_lFrequency = {0};
LARGE_INTEGER m_tStart = {0};
LARGE_INTEGER m_tEnd = {0};
double m_dTimeInterval = 0;
};
cpp文件
#include "stdafx.h"
#include "StaticTime.h"
#include "cmath"
CStaticTime::CStaticTime()
{
QueryPerformanceFrequency(&m_lFrequency);
}
CStaticTime::~CStaticTime()
{
}
void CStaticTime::TimingStart()
{
QueryPerformanceCounter(&m_tStart);
}
void CStaticTime::TimingEnd()
{
QueryPerformanceCounter(&m_tEnd);
m_dTimeInterval = ((double)m_tEnd.QuadPart - (double)m_tStart.QuadPart) * 1000 / (double)m_lFrequency.QuadPart;
}
double CStaticTime::GetInterval()
{
return m_dTimeInterval;
}
double CStaticTime::GetTicksCount()
{
LARGE_INTEGER currCounter;
QueryPerformanceCounter(&currCounter);
return (double)currCounter.QuadPart;
}
double CStaticTime::GetInterval(const double timeF, const double timeB)
{
double absInteval = abs(timeB - timeF);
return (absInteval * 1000) / (double)m_lFrequency.QuadPart;
}
上一篇: 解决CPU占用比较高的方法
下一篇: linux---同步和互斥的区别