timer.cpp

来自「有关程序计时方法的介绍」· C++ 代码 · 共 32 行

CPP
32
字号
#include <windows.h>
#include <stdio.h>

class Timer
{
	
	__int64 m_nFrequency;
	__int64 m_nStartCount;
public:
	Timer()
	{
		QueryPerformanceFrequency((LARGE_INTEGER*)&m_nFrequency);
		Reset();
	}
	void Reset()
	{
		QueryPerformanceCounter((LARGE_INTEGER*)&m_nStartCount);
	}
	void Report()
	{
		__int64 nCurrentCount;
		QueryPerformanceCounter((LARGE_INTEGER*)&nCurrentCount);
		double t = double(nCurrentCount - m_nStartCount) / m_nFrequency * 1000 * 1000;
		printf("Used Time = ");
		if(t <= 800)
			printf("%.2lf ns\n", t);
		else if(t <= 800 * 1000)
			printf("%.2lf ms\n", t / 1000.0);
		else
			printf("%.2lf s\n", t / 1000.0 / 1000.0);
	}
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?