六安束学龙被逮:vc中如何获取进程的CPU时间?
来源:百度文库 编辑:查人人中国名人网 时间:2024/05/10 19:13:56
就是在windows 2000以上版本中,任务管理器的“CPU时间”一栏。
ontimer是发送WM_TIMER消息来执行响应程序的。一般精确计时用这个不好,1s以上用这个还差不多。
Sleep同样是计时,但是效果比ontimer好多了,内部机制不一样.但是会阻塞界面线程。开一个线程来完成他!
如果你想精确到ns的话,我给你一段代码:
inline unsigned _int64 GetCycleCount( void )
{
_asm _emit 0X0F;
_asm _emit 0X31;
}
class CCountTimer
{
private:
unsigned _int64 m_startcycle;
public:
unsigned _int64 m_overhead;
CCountTimer(void)
{
m_overhead = 0;
Start();
m_overhead = Stop();
}
void Start(void)
{
m_startcycle = GetCycleCount();
}
unsigned _int64 Stop(void)
{
return GetCycleCount() - m_startcycle - m_overhead;
}
};
CCountTimer这个类可以得到函数执行期间,cpu所转动的次数。
给个使用例子:
CCountTimer timer;
timer.Start();
Sleep(1000);
UINT CpuSpeed = (UINT)timer.Stop();
CpuSpeed为1秒种cpu转动的次数
timer.Start();
//...dothing.....
UINT CountSpan = (UINT)timer.Stop();
CountSpan就是你执行某段代码cpu转动的次数
现在就是一道简单的数学题。已知cpu一秒转多少下。现在cpu转了这麽多下。问经过了多少秒?