GetTickCount() 函数的作用和用法
来源:互联网 发布:人工智能平台有哪些 编辑:程序博客网 时间:2024/05/21 18:49
DWORD GetTickCount(void);
1) 定义
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 usesGetTickCount to be easily tested for correct overflow handling.
在Debug版本中,设备启动后便从计时器中减去180秒。这样方便测试使用该函数的代码的正确溢出处理。
Return Values
The number of milliseconds indicates success.
返回值:如正确,返回毫秒数。
Header: windows.h.
Link Library: Coredll.lib.
2) 应用
用来计算某个操作所使用的时间:
...//执行耗时的操作
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使用的高精度时间函数,并要求计算机从硬件上支持高精度计时器。
以上引用:
http://www.wesoho.com/article.asp?id=2072
http://bitboy.blog.edu.cn/user1/19986/archives/2005/1001846.shtml
#include "stdafx.h"#include<cstdio>#include<cstdlib>#include<windows.h>void main() { DWORD dwLast; DWORD dwCurrent; DWORD dwInterval = 1000; dwLast = GetTickCount(); int i = 0; while(true) { dwCurrent = GetTickCount(); if( dwCurrent - dwLast < dwInterval ) continue; printf("dwLast,dwCurrent,diff:%d,%d,%d \n",dwLast,dwCurrent,dwCurrent-dwLast);for(int i=0;i<10000000;i++);dwCurrent = GetTickCount();printf("dwCurrent=%d\n",dwCurrent);break; } getchar(); return;}
DWORD dwCurrent = GetTickCount(); 获取系统当前时间 ms为单位的计算
C++ GetTickCount()和Sleep()
C++GetTickCount()和Sleep()
GetTickCount返回(retrieve)从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD。GetTickCount函数的原型为
- 【总结】GetTickCount() 函数的作用和用法
- GetTickCount() 函数的作用和用法!
- GetTickCount() 函数的作用和用法
- GetTickCount() 函数的作用和用法
- GetTickCount()函数的作用和用法
- SetTimer函数和GetTickCount()函数的用法
- SetTimer函数和GetTickCount()函数的用法
- 关于GetTickCount函数的用法
- rand()和srand()GetTickCount函数用法
- rand()和srand()GetTickCount函数用法
- GetTickCount() 函数的使用
- GetTickCount()函数的陷阱!
- 函数GetTickcount和DWORD关键字
- linux上的GetTickCount函数
- linux上的GetTickCount函数
- Linux上的GetTickCount函数
- 析构函数的作用和用法
- header() 函数的用法和作用
- Big Data, Apple Driving Industry's Biggest Salaries
- html5 视频和音乐播放器标签属性
- C++设计模式---桥接模式—合成/聚合复用原则
- ORALCE控制文件
- 1.点亮1个led程序分析(汇编)
- GetTickCount() 函数的作用和用法
- poj3624 01背包
- Linux设备驱动之I/O端口与I/O内存
- MFC加入滚动条
- 快速排序算法
- 大小为0的数组
- getch() vs getchar()
- uestc OJ 1849Negative People in Da House
- CKEditor+CKFinder配置以及使用简介