GetTickCount

GetTickCount,函数。GetTickCount返回(retrieve)从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD。

函数原型:
DWORD GetTickCount(void);
 
C++版
CString s;
DWORD k=::GetTickCount(); //获取毫秒级数目
int se = k/1000; // se为秒
cout<<se<<endl;
库文件:kernl32.dll
C/C++头文件:winbase.h
windows程序设计中可以使用头文件windows.h
 
 
//代替time函数来初始化随机数生成器
#include <iostream>
#include <windows.h>
#include <WinBase.h>
#include <ctime>
using namespace std;
int main()
{
int i, k, r;
for (i = 0; i < 10; i++)
{
srand (GetTickCount());
cout<<endl;
for (k = 0; k < 5; k++)
{
r = rand ();
cout<<r<<endl;
}
}
return 0;
}

3注意事项

GetTickcount函数:它返回从操作系统启动到当前所经过的毫秒数,常常用来判断某个方法执行的时间,其函数原型是DWORD GetTickCount(void),返回值以32位的双字类型DWORD存储,因此可以存储的最大值是2^32 ms约为49.71天,因此若系统运行时间超过49.71天时,这个数就会归0,MSDN中也明确的提到了:"Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days."。因此,如果是编写服务器端程序,此处一定要万分注意,避免引起意外的状况。
特别注意:这个函数并非实时发送,而是由系统每18ms发送一次,因此其最小精度为18ms。当需要有小于18ms的精度计算时,应使用StopWatch方法进行。
 
 
 

Best Regards To Reader:
==============================================
Royal Kao(高全宁)
Mob: 13771921045
Mail: gaoquanning@163.com
Blog: http://www.cnblogs.com/gaoquanning/
==============================================
原文地址:https://www.cnblogs.com/gaoquanning/p/3436258.html