欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;
}