Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 27,596 times

Contents

Related Categories

Performance Issues - Measuring Performance

Measuring Performance

When you make changes to your program, hoping to make it run faster, you need a way to accurately measure the time between certain events. You can do this by using the QueryPerformanceCounter and QueryPerformanceFrequency to measure this. As an example, lets use the following code:


Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Any) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Any) As Long
'// timer frequency
Dim secFreq As Currency

Private Sub cmdTimeIt_Click()
    Dim sec As Currency, secOut As Currency
    Dim I As Long
    '// start the timer
    ProfileStart sec
    '// do something
    For I = 0 To 1000000
        DoEvents
    Next
    ProfileStop sec, secOut
    MsgBox secOut
End Sub
Sub ProfileStart(secStart As Currency)
    '// if we do not have the timer frequency, then get it
    If secFreq = 0 Then QueryPerformanceFrequency secFreq
    '// get the current value
    QueryPerformanceCounter secStart
End Sub

Sub ProfileStop(secStart As Currency, secTiming As Currency)
    '// get the time passed
    QueryPerformanceCounter secTiming
    If secFreq = 0 Then
        '// there is no high-resolution timer available.
        secTiming = 0
    Else
        '// calculate the time taken from the start value, finish value, and the timer frequency.
        secTiming = (secTiming - secStart) / secFreq
    End If
End Sub

As you can see, we pass the ProfileStart procedure an empty Currency variable (sec) to hold the start value. When the code you want to run is finished, call the ProfileStop procedure with the start value (sec), and another empty Currency variable (secOut). The value contained in secOut is the time taken. You usually get the time taken, accurate to the nearest microsecond. As you can see, this is invaluable when trying to see the changes in performance as you change your code.

We can also use GetTickCount API for Measuring Performance. The following code gives you an example.

Private Sub TimeOperation()
    Dim I as Long
    Dim x as Long
    Dim Y as Long
    Dim Z as Double

    X = GetTickCount()
    For I = 0 to 1000000
        Z = SIN(i)
    Next I

    Y = GetTickCount - x
    MsgBox "This operation takes " & Y/1000 & " seconds to finish'
End Sub

Thanks to Georgi Ganchev for this tip.

For more information on this, and other methods for timing code, search for 'HOWTO: Use QueryPerformanceCounter to Time Code' in the MSDN library.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Once Again!

    Posted by MahR on 29 Sep 2005

    And once again a greate article by james!

  • Use of ByRef and ByVal

    Posted by JonathanEvans on 21 Mar 2002

    This posting is specifically a response to James Cowleys suggestion that using ByVal is quicker than using ByRef. This is only true when calling an out-of-process server (i.e. and ActiveX EXE). VB i...