毫秒级计时,用Timer能实现吗

秋天之落叶 2019-09-02 06:19:56
假如我是 12:30:30 123开始计时,我想用时5分钟,能不能精确到12:35:30 123结束,如何操作,如何判断?
我目前在网上找的方法都不能精确到000毫秒,多少都会差点,即使我的timer.interval:=1也不行。
...全文
2011 43 打赏 收藏 转发到动态 举报
写回复
用AI写文章
43 条回复
切换为时间正序
请发表友善的回复…
发表回复
AQing阿清 2019-09-22
  • 打赏
  • 举报
回复
抢答器不能用windows来实现,因为windows是非实时操作系统。这样的工作需要用单片机来实现,单片机可以做到实时
liuluckystar 2019-09-07
  • 打赏
  • 举报
回复
不可能,无论如何都会有误差!
StarryFish 2019-09-07
  • 打赏
  • 举报
回复
不可能,无论如何都会有误差!
  • 打赏
  • 举报
回复
在我的电脑上(win10+6700k),输出:

time elapsed 0.00010s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00125s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00124s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00123s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00101s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00101s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00363s (QueryPerformanceCounter), 0.00400s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00011s (QueryPerformanceCounter), 0.00000s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00013s (QueryPerformanceCounter), 0.00000s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00009s (QueryPerformanceCounter), 0.00000s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00122s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.01500s (GetTickCount)
time elapsed 0.00011s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00124s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00100s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00114s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00124s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00123s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00100s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00245s (QueryPerformanceCounter), 0.00200s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00006s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00001s (QueryPerformanceCounter), 0.00000s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00102s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00100s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00114s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00117s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00130s (QueryPerformanceCounter), 0.00200s (timeGetTime), 0.01600s (GetTickCount)
time elapsed 0.00015s (QueryPerformanceCounter), 0.00000s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00183s (QueryPerformanceCounter), 0.00200s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00011s (QueryPerformanceCounter), 0.00000s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00090s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00257s (QueryPerformanceCounter), 0.00200s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00007s (QueryPerformanceCounter), 0.00000s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00120s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00115s (QueryPerformanceCounter), 0.00200s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00009s (QueryPerformanceCounter), 0.00000s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00101s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)
time elapsed 0.00111s (QueryPerformanceCounter), 0.00100s (timeGetTime), 0.00000s (GetTickCount)


注意这是操作系统+硬件相关的,在不同的电脑上结果可能不一样,你不要质疑我的人格~~~
  • 打赏
  • 举报
回复
老赵,可以明确地说,你的知识还停留在10+年以前,大约09年(或者08年?)开始的core架构处理器支持Invariant TSC(实际上还有一个rdrand指令生成真随机数很少有人知道 :),时钟计数与运行频率无关而且所有核心同步,win7+使用这个技术来获取更高的计时精度,所以timeSetEvent、timeGetTime是可以精确到<= 1ms的,但是GetTickCount仍然使用老的计时方法,只能精确到15ms(准确值是1/64秒,也就是 windows线程调度器的中断率)。注意在windows使用Invariant TSC计时的情况下,QueryPerformanceFrequency会使用LAPIC时钟,通常只有几MHz(输出一下QueryPerformanceFrequency的结果,如果是几GHz就是用了TSC,如果是几MHz则是用了HPET/LAPIC时钟),所以在这种情况下QueryPerformanceCounter精度其实不如timeGetTime。
考虑到你可能不会Delphi,我写了一个C代码测试程序:

#include <stdio.h>
#include <windows.h>

#pragma comment(lib, "winmm")

UINT TimerID;
ULONGLONG PF, PC;
DWORD TT, TC;

void TimerCallbackProc(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
ULONGLONG PC1, PC2;
DWORD T1, T2, TC1, TC2;

QueryPerformanceCounter((LARGE_INTEGER *)&PC1);
T1 = timeGetTime();
TC1 = GetTickCount();
PC2 = PC1 - PC, T2 = T1 - TT, TC2 = TC1 - TC;
PC = PC1, TT = T1, TC = TC1;
printf("time elapsed %.5fs (QueryPerformanceCounter), %.5fs (timeGetTime), %.5fs (GetTickCount)\n",
(double)PC2 / PF, T2 / 1000.0, TC2 / 1000.0);
}


int main()
{
QueryPerformanceFrequency((LARGE_INTEGER *)&PF);
TimerID = timeSetEvent(1, 0, &TimerCallbackProc, 0, TIME_PERIODIC);
QueryPerformanceCounter((LARGE_INTEGER *)&PC);
TT = timeGetTime();
TC = GetTickCount();
getchar();
timeKillEvent(TimerID);

return 0;
}

秋天之落叶 2019-09-06
  • 打赏
  • 举报
回复
引用 38 楼 早打大打打核战争 的回复:
注意这是操作系统+硬件相关的,在不同的电脑上结果可能不一样,你不要质疑我的人格~~~

我们的办公用机,i3六代CPU,测试结果timegettime比GetTickCount精确多了,基本上是1毫秒,有时候第一次运行能到16毫秒,但再运行基本都是1毫秒。GetTickCount就基本没有什么规律了。这是怎么回事?
这不是鸭头 2019-09-06
  • 打赏
  • 举报
回复
做不到,不光windows做不到,linux系统也做不到,这些都不是实时操作系统。
wzc1006847216 2019-09-05
  • 打赏
  • 举报
回复
可以根据采样频率去计时
秋天之落叶 2019-09-05
  • 打赏
  • 举报
回复
引用 28 楼 S_clifftop 的回复:
你要把值存起来,每次到时间,减掉5分钟,然后剩下的值继续计算

“到时间”,这是关键,这个就实现不了,哈哈
赵4老师 2019-09-05
  • 打赏
  • 举报
回复
或者你就来一句
Sleep(1);
然后用performance counter计时,看看实际过了多少毫秒。
赵4老师 2019-09-05
  • 打赏
  • 举报
回复
windows切换线程时使用的计时器不是performance counter。 我瞎猜。
赵4老师 2019-09-05
  • 打赏
  • 举报
回复
引用 33 楼 早打大打打核战争 的回复:
[quote=引用 32 楼 赵4老师 的回复:] Windows计时精度≈15毫秒
那是在很早期的硬件上 [/quote] 你不信让timer延时1毫秒,用performance counter计时,看看实际延时了多少毫秒。
  • 打赏
  • 举报
回复
引用 32 楼 赵4老师 的回复:
Windows计时精度≈15毫秒


那是在很早期的硬件上
赵4老师 2019-09-05
  • 打赏
  • 举报
回复
Windows计时精度≈15毫秒
luj_1768 2019-09-05
  • 打赏
  • 举报
回复
与线程运行权限有关。需要使用系统级调用函数。
S_clifftop 2019-09-04
  • 打赏
  • 举报
回复
你要把值存起来,每次到时间,减掉5分钟,然后剩下的值继续计算
S_clifftop 2019-09-04
  • 打赏
  • 举报
回复
不可能,无论如何都会有误差
  • 打赏
  • 举报
回复
如果你要只触发一次,把TIME_PERIODIC换成TIME_ONESHOT
sichuanwww 2019-09-04
  • 打赏
  • 举报
回复
需要硬件计时器或实时系统
  • 打赏
  • 举报
回复
多媒体计时器是在系统线程中运行的,所以即便回调所在的用户线程当前处于挂起状态,回调也能得到执行。
加载更多回复(23)
#### 收益: 学习《optee系统开发精讲 》套餐的收益如下:1、全体系的掌握optee的核心知识点(多核多线程、启动流程、各类API、TA/PTA/STA、driver、service、各类设计思想、内存管理、中断管理...);2、掌握TEE OS的设计核心思想(不限与optee,对其它TEE也是如此),能够进入软件架构层次的各项设计;3、快速熟悉大系统软件的各项组件,能够进行大系统软件之间的设计或排查问题4、学习和理解理解Armv8/Armv9架构、Trustzone架构5、技术水平提升N个level, 掌握快速的学习方法;#### 课程简介和大纲 该课程包含但不限与以下章节:optee的组件介绍optee os启动流程optee os的初始化流程optee os线程向量表及其作用进入optee os的八种方式optee os的异常向量表和设计模型optee os对irq中断的处理optee os对fiq中断的处理optee os的内存管理optee os的MMU的使用optee os的根密钥(信任根)optee os的RPC机制optee os的存储系统optee os的SFS系统optee os的RPMB机制的实现optee os的Timer实现optee os中实现的GP APIoptee os中的系统调度optee os中密码学函数的支持optee os中的线程调度CA/TA的开发模型optee os中的serviceoptee os中的 PTAoptee os中的 secure driveroptee os中的 Early TAoptee中spinlock的实现原理详解optee中mutex的实现方式optee的栈指针和栈内存的介绍optee中使用虚函数(平台客制化)的设计模型共享内存的介绍optee os的标准错误码TA的签名和验签TA链接静态库的方法

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧