函数的返回类型是DWORD,函数本身功能:返回OS开启在现在所经过的毫秒数(ms).
DWORD GetTickCount();
//函数的返回类型是DWORD,函数本身功能:返回OS开启在现在所经过的毫秒数(ms).
long now = GetTickCount();
if (m_nTickRoomInfo != 0 && (now - m_nTickRoomInfo >= 60000))
{
m_nTickRoomInfo = now;
CCLog("Update ObtainRoomInfo");
HttpInvoke::ObtainRoomInfo();
}
用处:(1.)
判断某个方法被调用的时间间隔。
long now = GetTickCount();
if (m_nTickRoomInfo != 0 && (now - m_nTickRoomInfo >= 60000)){
m_nTickRoomInfo = now;
CCLog("Update ObtainRoomInfo");
HttpInvoke::ObtainRoomInfo();
}
用处:(2.)
随机数的生成。srand(::GetTickCount());
不过这种随机数生成有个问题:往往会产生伪随机,两次执行之间时间间隔非常短,里面会有很多数字重复。
srand(::GetTickCount());
for(int i = 0 ;i < 100 ;++i)
{
int m_nrand = rand();
}
解决办法:
srand((unsigned int)GetCpuCycle());
int x = rand() % 9 + 1; //获取1-9之间的一个随机数
//获得CPU自上电以来的时间周期数
__declspec (naked) unsigned __int64 GetCpuCycle()
{
_asm
{
rdtsc
ret
}
}
上面获得时间周期数的代码是汇编语言所写,可放到所使用文件的最上面。