程序计时类

/***************************************************
 ***************************************************
  TimeCount.h -- interface of the 'TimeCount' general purpose time count library
  version 1.0.8, Jan 15th, 2016

  Copyright (C) 2015-2016 Acheld CHEN

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Acheld CHEN       
  http://blog.csdn.net/acheld
  
 ***************************************************
 ****************************************************/


#pragma once
class CTimeCount
{
private:
	CTimeCount(void);
	~CTimeCount(void);
public:
	static void Init(CTimeCount* &pTime);
	static void GetSystemTimeMsg(LPSTR lpMsg);
	static void Tick(LONGLONG& Qpart1);
	static void Tick(LONGLONG& Qpart1,LPSTR lpSpendTime = NULL);
	static void Tock(LONGLONG& Qpart1,LPSTR lpSpendTime);
	static void ComputeTime(LONGLONG &Qpart1 /*input*/,double& dTime /*output*/);
	
	void TickTockExample();
private:
	static void GetFrequency(double& dFreq);
	static void QueryCounter(LONGLONG& longData);
	


};


/************************************** 
Acheld CHEN       
<a target=_blank href="http://blog.csdn.net/acheld">http://blog.csdn.net/acheld</a>
  
 ***************************************************
 ****************************************************/


#include "StdAfx.h"
#include "TimeCount.h"

static int m_nCount = 0;

CTimeCount::CTimeCount(void)
{
}


CTimeCount::~CTimeCount(void)
{
}

void CTimeCount::Init( CTimeCount* &pTime )
{
	pTime = NULL;
	if (!pTime)
	{
		pTime = new CTimeCount();
	}
}

void CTimeCount::GetSystemTimeMsg(LPSTR lpMsg)
{
	//get system time
	SYSTEMTIME systemTime;
	memset((void*)&systemTime,0,sizeof(SYSTEMTIME));
	GetSystemTime(&systemTime);
	char lpSystemTime[256] = "\0";
	sprintf(lpSystemTime,"[ %4d.%2d.%2d  %2d:%2d:%2d ]",systemTime.wYear,\
		systemTime.wMonth,systemTime.wDay,systemTime.wHour+8,\
		systemTime.wMinute,systemTime.wSecond);

	char lpstr[512] = "\0";
	strcpy(lpstr,lpSystemTime);
	strcpy(lpMsg,lpstr);
}

void CTimeCount::Tick(LONGLONG& Qpart1)
{
	QueryCounter(Qpart1);
}

void CTimeCount::Tick( LONGLONG& Qpart1,LPSTR lpSpendTime /*= NULL*/ )
{
	//if (lpSpendTime == NULL){}
	QueryCounter(Qpart1);
}

void CTimeCount::GetFrequency( double& dFreq )
{
	LARGE_INTEGER litmp;

	QueryPerformanceFrequency(&litmp);
	dFreq = (double)litmp.QuadPart;
}

void CTimeCount::QueryCounter( LONGLONG& Qpart )
{
	LARGE_INTEGER litmp;

	QueryPerformanceCounter(&litmp);
	Qpart = litmp.QuadPart;
}

void CTimeCount::ComputeTime(LONGLONG &Qpart1 /*input*/,double& dTime /*output*/)
{
	LONGLONG Qpart2;
	double dMinus,dFreq;

	//do something
	QueryCounter(Qpart2);
	GetFrequency(dFreq);
	dMinus = (double)(Qpart2-Qpart1);
	dTime = dMinus/dFreq *1000; //ms,microSecond
	///dTime = dMinus/dFreq //s,second
}

void CTimeCount::Tock(LONGLONG& Qpart1,LPSTR lpSpendTime)
{
	double dTime;
	ComputeTime(Qpart1,dTime);
	char lpTime[256] = "\0";
	char lpMsg[512] = "\0";
	GetSystemTimeMsg(lpMsg);
	sprintf(lpTime,"  Cost Time: %lf ms  ",dTime);
	strcpy(lpSpendTime,lpMsg);
	strcat(lpSpendTime,lpTime);
	strcat(lpSpendTime,"\n");
}

void CTimeCount::TickTockExample()
{
	LONGLONG Qpart1;
	Tick(Qpart1,NULL);

	//do something
	Sleep(560);

	char lpSpendTime[256] = "\0";
	Tock(Qpart1,lpSpendTime);

}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值