Windows API GetTickCount函数

来源:互联网 发布:js修改div style 编辑:程序博客网 时间:2024/05/18 01:33


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

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."。因此,如果是编写服务器端程序,此处一定要万分注意,避免引起意外的状况。

 

//代码片段//库文件:kernl32.dll   C/C++头文件:winbase.h DWORD k=::GetTickCount(); //获取毫秒级数目   int se = k/1000; // se为秒   cout<<se<<endl;   
//代替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());        for (k = 0; k < 5; k++)       {                r = rand ();             cout<<r<<endl;        }     }   return 0;}

 


 

 

原创粉丝点击