-
Timer Resolution
Hi all,
I am trying to find out what the resolution is of the timer in VB. By resolution, I mean the smallest number in milliseconds that it can be defined for. I am also looking for the disadvantages of this resolution. I think that the main disadvantage is that it makes it difficult to use VB in a real time application, but I am not sure.
Thanks
-
Re: Timer Resolution
Here is an API to count milliseconds
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Sub Command1_Click()
Dim lngStartTime As Long
Dim lngFinishTime As Long
lngStartTime = timeGetTime
' Here you usualyt put your sub calls that needs to be timed
' For example's sake I just put in a loop
Dim I As Integer
For I = 1 To 30000
Next I
lngFinishTime = timeGetTime
MsgBox ("This task took " & lngFinishTime - lngStartTime & " milliseconds!")
End Sub
Iouri Boutchkine
[email protected]
-
Re: Timer Resolution
The smallest resolution for a timer in VB is 1 ms. Keep in mind that VB is singlethreaded, so if you execute code in the timerevent, the next timerevent can only be fired as soon as the previous has ended. This could be one of those disadvantages you are looking for.
Using VB in a realtime application? It depends, if the application is realy time critical (wich I can imagine, why else would you ask the question) VB probably isn't the best solution. However if the difference between 10 ms and 20 ms isn't that big of a deal, VB can do the job.
Tom Cannaerts
[email protected]
The best way to escape a problem, is to solve it.
-
Re: Timer Resolution
There is a solution wich is more accurate then milliseconds:
A counter is a general term used in programming to refer to an incrementing variable. Some systems include a high-resolution performance counter that provides high-resolution elapsed times.
Source: MSDN
If a high-resolution performance counter exists on the system, the QueryPerformanceFrequency function can be used to express the frequency, in counts per second. The value of the count is processor dependent. On some processors, for example, the count might be the cycle rate of the processor clock.
The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter. By calling this function at the beginning and end of a section of code, an application essentially uses the counter as a high-resolution timer. For example, suppose that QueryPerformanceFrequency indicates that the frequency of the high-resolution performance counter is 50,000 counts per second. If the application calls QueryPerformanceCounter immediately before and immediately after the section of code to be timed, the counter values might be 1500 counts and 3500 counts, respectively. These values would indicate that .04 seconds (2000 counts) elapsed while the code executed.
The APIs that come with that are
public Declare Function QueryPerformanceCounter Lib "kernel32" Alias "QueryPerformanceCounter" (lpPerformanceCount as LARGE_INTEGER) as Long
public Declare Function QueryPerformanceFrequency Lib "kernel32" Alias "QueryPerformanceFrequency" (lpFrequency as LARGE_INTEGER) as Long
public Type LARGE_INTEGER
lowpart as Long
highpart as Long
End Type
Tom Cannaerts
[email protected]
The best way to escape a problem, is to solve it.
-
Re: Timer Resolution
Since the timer ticks 18.2 times a second (91 times in 5 seconds), the resolution of a timer is 1/18.2 seconds, or approximately 55 ms. Making the timer interval smaller than this is of no use.
-
Re: Timer Resolution
Great. Thanks for sharing
(out of vote, for today...)
Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
-
Re: Timer Resolution
I've put a working example of the GetPerformanceCount API on http://www.planetsourcecode.com/xq/A...s/ShowCode.htm
This also illustrates that the GetTickCount API isn't as fast as it may seem.
Tom Cannaerts
[email protected]
The best way to escape a problem, is to solve it.
-
Re: Timer Resolution
High resolution timer(VB source code include):
http://www.banasoft.com/DownLoad/BNTimer.exe