您的位置:首页 > 编程语言

获取进程运行的时间源代码

2009-01-21 11:11 288 查看
#include <Windows.h>
#include <iostream>
using namespace std;
typedef void (*RUNFUN)();
DWORD GetFunRunTime(RUNFUN pfun);
void Run();
class CStopWatch
{
public:
CStopWatch()
{
QueryPerformanceFrequency(&m_liPerfFreq);
Start();
}
~CStopWatch()
{
}
public:
void Start()
{
QueryPerformanceCounter(&m_liPerfStart);
}
__int64 Now() const
{
LARGE_INTEGER liPerfNow = {0};
QueryPerformanceCounter(&liPerfNow);
return ((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000) / m_liPerfFreq.QuadPart;
}
private:
LARGE_INTEGER m_liPerfFreq;
LARGE_INTEGER m_liPerfStart;
};
int main()
{
CStopWatch objWatch;
cout << "Run() use time: " << GetFunRunTime(Run) << endl;
cout << "process use time: " << objWatch.Now() << endl;
return 0;
}
DWORD GetFunRunTime(RUNFUN pfun)
{
DWORD dwStartTime = 0;
DWORD dwEndTime = 0;
dwStartTime = GetTickCount();
pfun();
dwEndTime = GetTickCount();
return dwEndTime - dwStartTime;
}
void Run()
{
int i = 0;
for (i = 0; i < 100; i++)
{
cout << "Hello, andylin!" << endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: