QTimer和Qthread的调度时间精度
来源:互联网 发布:类似clover的软件 编辑:程序博客网 时间:2024/04/29 21:48
最近做的一个模拟嵌入式设备的项目中,要求事件的响应精度在1毫秒左右,特地编写代码测试了一下QTimer和QThread中的msleep函数的时间精度。
QT的帮助中对于QTimer的时间精度问题是这么写的:
Timers will never time out earlier than the specified timeout value and they are not guaranteed to time out at the exact value specified. In many situations, they may time out late by a period of time that depends on the accuracy of the system timers.
The accuracy of timers depends on the underlying operating system and hardware. Most platforms support a resolution of 1 millisecond, though the accuracy of the timer will not equal this resolution in many real-world situations.
If Qt is unable to deliver the requested number of timer clicks, it will silently discard some.
测试函数用到了windows的高精度时间读取函数,如下所示
#include <Windows.h>
#include <math.h>
#define TIMER_INTVL 1000 //毫秒
#define ARRAY_LEN 1 //数组长度
//传入调用时间间隔,打印出最大和平均时间误差void testTimer(int intvl_us){static bool inited = false;static LARGE_INTEGER lastT;static LARGE_INTEGER freq;LARGE_INTEGER now;static int usarray[ARRAY_LEN];static int index = 0;static int maxus = 0, averus = 0, difus;//时间差QString info("最大时间差:");if(!inited){memset(usarray, 0, sizeof(int)*ARRAY_LEN);QueryPerformanceCounter(&lastT);//获取第一次进入时的时间QueryPerformanceFrequency(&freq);//获取时钟频率inited = true;return;}QueryPerformanceCounter(&now);difus = ((now.QuadPart-lastT.QuadPart)*1000000)/freq.QuadPart;difus = abs(difus-intvl_us);usarray[index++] = difus;maxus = maxus>difus?maxus:difus;if(index == ARRAY_LEN){index = 0;for(int i=0; i<ARRAY_LEN; i++)averus += usarray[i];averus /= ARRAY_LEN;info = info + QString::number(maxus) + " 平均误差 " + QString::number(averus);gSimDrvDlg->putInfo(info);maxus = 0;averus = 0;}lastT = now;}
把此函数设为QTimer的超时响应函数,在32位windows7下测试QTimer的不同定时周期的调度误差如下:
- QTimer和Qthread的调度时间精度
- 使用QThread和QTimer的发现
- QThread与QTimer使用注意
- Qt QTimer测试定时精度
- QThread之调度
- Qt paintEvent和QTimer更新的问题
- Qt中利用定时器QTimer准实时显示当前日期和时间(有自己的思考)
- 关于JavaScript的时间精度
- 计算机计算的时间精度
- Wince的时间精度问题
- Wince的时间精度问题
- 获取不同精度的时间
- QThread的使用要点和例子
- QTimer和QBasicTimer使用
- QTime获取当前时间,QDateTime获取日期和时间,QTimer定时器,QLCDNumber数码显示框
- 时间调度的架构
- Qt定时器:QObject类的定时器和定时器类QTimer
- QTimer::remainingTime()在win7和CentOS6.5上的差异
- 宏
- 【redhat5.5】软件安装卸载方法
- 位运算
- Android 开发指南 翻译11 Application Resources -- Drawable Resources
- NHibernate Restrictions用法
- QTimer和Qthread的调度时间精度
- 1002
- IPHONE应用开发 - 标签栏
- 变量取值范围
- IP/ARP/ICMP/UDP 一些原理
- Android架构详解
- iPhone开发学习笔记006—— NSNotification自定义通知名
- 转义字
- 中国人才报告透露未来几年可能走红专业