服务器什么建网站,龙港哪里有做阿里巴巴网站,广西南宁网站建设有限公司,建站公司哪个平台最好GetTickCount是一种函数。GetTickCount返回#xff08;retrieve#xff09;从操作系统启动所经过#xff08;elapsed#xff09;的毫秒数#xff0c;它的返回值是DWORD。 GetTickcount函数#xff1a;它返回从操作系统启动到当前所经过的毫秒数#xff0c;常常用来判断某…GetTickCount是一种函数。GetTickCount返回retrieve从操作系统启动所经过elapsed的毫秒数它的返回值是DWORD。 GetTickcount函数它返回从操作系统启动到当前所经过的毫秒数常常用来判断某个方法执行的时间其函数原型是DWORD GetTickCount(void)返回值以32位的双字类型DWORD存储因此可以存储的最大值是(2^32-1) ms约为49.71天因此若系统运行时间超过49.71天时这个数就会归0MSDN中也明确的提到了:“Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.”。因此如果是编写服务器端程序此处一定要万分注意避免引起意外的状况如需避免此种情况可使用Ctime类或者是系统API的SYSTEMTIME进行判断。 特别注意这个函数并非实时发送而是由系统每18ms发送一次因此其最小精度为18ms。当需要有小于18ms的精度计算时应使用StopWatch方法进行。
连续触发200次实测下来最小间隔在15ms。实际状况应该是系统每秒触发64次间隔在15、16ms之间波动。//代替time函数来初始化随机数生成器 #include #include windows.h #include WinBase.h #include using namespace std; int main() { int i, k, r; for (i 0; i 10; i) { srand (GetTickCount()); coutendl; for (k 0; k 5; k) { r rand (); coutrendl; } } return 0; }
DWORD GetTickCount(void);
定义 For Release configurations, this function returns the number of milliseconds since the device booted, excluding any time that the system was suspended. GetTickCount starts at 0 on boot and then counts up from there. 在Release版本中该函数从0开始计时返回自设备启动后的毫秒数不含系统暂停时间。 For Debug configurations, 180 seconds is subtracted from the the number of milliseconds since the device booted. This allows code that uses GetTickCount to be easily tested for correct overflow handling. 在Debug版本中设备启动后便从计时器中减去180秒。这样方便测试使用该函数的代码的正确溢出处理。 Return Values The number of milliseconds indicates success. 返回值如正确返回毫秒数。 Header: Winbase.h. Link Library: Coredll.lib.应用 用来计算某个操作所使用的时间
Start:GetTickCount; …//执行耗时的操作 Stop:GetTickCount; TimeUsed:(Stop-Start)/1000; //使用了xxx秒
用来定时
void main() { DWORD dwLast; DWORD dwCurrent; DWORD dwInterval 1000; dwLast GetTickCount(); int i 0; while(true) { dwCurrent GetTickCount(); if( dwCurrent - dwLast dwInterval ) continue; //your code to be executed when interval is elapsed printf(dwLast,dwCurrent,diff:%d,%d,%d ,dwLast,dwCurrent,dwCurrent-dwLast); //your code to determine when to break if( i 10 ) break; i; dwLast dwCurrent; printf(“Time is up!”); break; } getchar(); return; }
对于一般的实时控制使用GetTickCount()函数就可以满足精度要求但要进一步提高计时精度就要采用QueryPerformanceFrequency()函数和QueryPerformanceCounter()函数。这两个函数是VC提供的仅供Windows 9X使用的高精度时间函数并要求计算机从硬件上支持高精度计时器。