如何实现微秒级的Timer?

lingwen 2000-05-09 07:05:00
可以做信号源用的.(vb的timer是毫秒级的,用来作信号源不太实际.)
...全文
607 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuyu 2000-06-20
  • 打赏
  • 举报
回复
在WIN9X系统下不太可能实现,
可以用硬件时钟
risings 2000-06-11
  • 打赏
  • 举报
回复
程序员大本营中好象有改进TIMER的控件。
Chen_Lin 2000-06-01
  • 打赏
  • 举报
回复
'Use this code to create a class called HiResTimer:

'The number is codified as HighPart*2^32+LowPart
Private Type LARGE_INTEGER
LowPart As Long
HighPart As Long
End Type

Private Declare Function QueryPerformanceCounter Lib _
"kernel32" (lpPerformanceCount As LARGE_INTEGER) _
As Long
Private Declare Function QueryPerformanceFrequency Lib _
"kernel32" (lpFrequency As LARGE_INTEGER) As Long

Private m_TicksPerSecond As Double
Private m_LI0 As LARGE_INTEGER
Private m_LI1 As LARGE_INTEGER

Friend Sub Class_Initialize()
Dim LI As LARGE_INTEGER

If QueryPerformanceFrequency(LI) <> 0 Then
m_TicksPerSecond = LI2Double(LI)
Else
m_TicksPerSecond = -1
End If
End Sub

Friend Property Get Resolution() As Double
Resolution = 1# / m_TicksPerSecond
End Property

Friend Sub EnterBlock()
QueryPerformanceCounter m_LI0
End Sub
Friend Sub ExitBlock()
QueryPerformanceCounter m_LI1
End Sub

Friend Property Get ElapsedTime() As Double
Dim EnterTime As Double, ExitTime As Double

EnterTime = LI2Double(m_LI0) / m_TicksPerSecond
ExitTime = LI2Double(m_LI1) / m_TicksPerSecond
ElapsedTime = ExitTime - EnterTime
End Property

Friend Function LI2Double(LI As LARGE_INTEGER) As Double
Dim Low As Double
Const TWO_32 = 4# * 1024# * 1024# * 1024#

Low = LI.LowPart
If Low < 0 Then Low = Low + TWO_32
'Now Low is in the range 0...2^32-1

LI2Double = LI.HighPart * TWO_32 + Low
End Function

Here’s an example of the HiResTimer in use:

Dim hrt As HiResTimer, d As Double
Set hrt = New HiResTimer
Debug.Assert hrt.Resolution > 0
MsgBox "Resolution [usecs]:" & hrt.Resolution * 1000000#

hrt.EnterBlock
hrt.ExitBlock
MsgBox "Call overhead [usecs]:" & hrt.ElapsedTime * _
1000000#

hrt.EnterBlock
d = 355# / 113#
hrt.ExitBlock

MsgBox "Elapsed Time [usecs]:" & hrt.ElapsedTime * _
1000000#

Believe it or not, you can time even native-compiled code division. For more information, look at the MSDN Library description of the kernel APIs used here. On x86 architectures, resolution is better that 1 microsecond. Be careful, however, of trusting single instance timings, as you’ll find the “resolution” of this performance counter varies over time. In fact, the overhead of simply calling QueryPerformanceCounter in VB is quite a measurable time period itself.
Although you can time single operations, you’re still better off averaging the time required for hundreds or thousands of similar operations.
—Alessandro Coppo, Rapallo, Italy
---------------------------------------------------------
From VBPJ
chengt 2000-05-29
  • 打赏
  • 举报
回复
Timer还受cpu当前空闲状态限制,cpu忙时,timer并不能接受到消息.
xubin_sh 2000-05-29
  • 打赏
  • 举报
回复
计算机达不到,操作系统更达不到
NowCan 2000-05-29
  • 打赏
  • 举报
回复
这两个函数能用吗?
QueryPerformanceFrequency
QueryPerformanceCounter
wjf 2000-05-29
  • 打赏
  • 举报
回复
有多媒体时钟可以一试
zjf_fly 2000-05-29
  • 打赏
  • 举报
回复
有源程:http://www.softunion.net/blackcat/stopwatch.zip
sanhan 2000-05-28
  • 打赏
  • 举报
回复
Timer是一定不能用的,只能试验一下查询方式。
还要考虑硬件接口的延时。在Dos下要方便多了。

为什么不用硬件直接做呢?
No9 2000-05-28
  • 打赏
  • 举报
回复
好像在DOS下可以直接使用中断来实现,我以前编过一个类似的小程序,但在WIN9x中我没有试过,我想如“Wonny”所说可能性不大!但也不是一定不能!
ecai 2000-05-27
  • 打赏
  • 举报
回复
可否考虑嵌入ASM语句实现?
Wonny 2000-05-27
  • 打赏
  • 举报
回复
1微秒=10E-6 秒;假设您的 CPU 主频为 450M。450M*10E-6=450。您得保证您的指令少于450条机器指令,而且CPU专门为您的程序服务,现在也许不太可能。
sanhan 2000-05-26
  • 打赏
  • 举报
回复
没有现成的函数可以实现。

但是你可以设置一个指令循环来实现这个目的。定时器用来测定你CPU的速度。
但这样不保证精确定时。要求低的时候还行。
qxm 2000-05-26
  • 打赏
  • 举报
回复
好象是为settimerevent的api,
Chen_Lin 2000-05-10
  • 打赏
  • 举报
回复
yeah!
http://www.csdn.net/visual%20basic/index.htm
StopWatch.zip
VB 提供的 Timer 控件的精度好象可以达到 1 毫秒,但实际上只能达到几十毫秒。此“跑表”类使用多媒体的定时器而真正提供精确到 1 毫秒的定时
已经很不错了。
茂奇软件 2000-05-10
  • 打赏
  • 举报
回复
The design for the interrupt of the system in
both Windos and Linux OS are base on ms level.

I don't think a software have this ability to
implement the interupte on ns level.

What we must make some hardware and device driver
and use the query to analyze the ns time. But it
will cost a lot of resource of your computer.

Regards Jansen Zhu.
larryxie 2000-05-10
  • 打赏
  • 举报
回复
ltp3 2000-05-10
  • 打赏
  • 举报
回复
关注
ltp3 2000-05-10
  • 打赏
  • 举报
回复
关注
lingwen 2000-05-09
  • 打赏
  • 举报
回复
能不能说详细点?
加载更多回复(1)

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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