CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2001
    Location
    PA, USA
    Posts
    3

    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


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    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]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    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.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    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.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  5. #5
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    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.


  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Timer Resolution

    Great. Thanks for sharing
    (out of vote, for today...)


    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    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.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  8. #8
    Join Date
    Jul 2000
    Posts
    110

    Re: Timer Resolution

    High resolution timer(VB source code include):

    http://www.banasoft.com/DownLoad/BNTimer.exe


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured