程序计时类
2016-01-15 22:04
316 查看
/*************************************************** *************************************************** 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);
}
相关文章推荐
- Matlab中计算程序运行时间的三种方法
- C# 记录程序耗时
- 【opencv练习05 - 计时器的使用】
- Linux用户态程序计时方式详解
- c++获取程序运行时间
- C#基础练习(事件登陆案例)
- C#基础练习(事件登陆案例)
- 读驱动ARK之A盾代码的总结-- 发文于2013-9-5
- Laravel 向视图传递变量的3种方法
- 初探C++流
- C/C++小知识
- Educational Codeforces Round 5 D. Longest k-Good Segment
- java加密、解密技术系列:Base64
- python学习第十课续 :线程池
- 怎样学习java?
- C#基础练习(时间的三连击)
- C#基础练习(时间的三连击)
- 《笨办法学Python》 第13课手记
- 利用sort函数对数组进行排序【java】
- Android生存指南:Eclipse快捷键