/***************************************************
***************************************************
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);
}