获取高精度时间差(Delphi)

本文介绍了Windows环境下两种获取时间间隔的方法:GetTickCount和QueryPerformanceCounter。前者精度受限于系统定时器,一般为10到16毫秒;后者提供更高的时间分辨率,但需要注意多处理器系统中可能出现的问题。

由于GetTickCount精度只有10~16ms

MSDN 写道
The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds.

 当程序要更高精度时间差的话,可以用QueryPerformanceCounter和QueryPerformanceFrequency

beginTick := 0;
endTick := 0;
expTime := 0;
QueryPerformanceCounter(beginTick);
// do something...
QueryPerformanceCounter(endTick);
QueryPerformanceFrequency(freq);
expTime := (endTick - beginTick) / freq * 1000;
 

不过QueryPerformanceCounter有个多CPU的问题要小心

MSDN 写道
On a multiprocessor computer, it should not matter which processor is called. However, you can get different results on different processors due to bugs in the basic input/output system (BIOS) or the hardware abstraction layer (HAL).
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值