- 注册时间
- 2011-3-6
- 最后登录
- 1970-1-1
该用户从未签到
|
发表于 2014-7-16 08:41:14
|
显示全部楼层
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转了这麽多下。问经过了多少秒? |
|