Visual Basic Concepts

Measuring Performance

Determining the best algorithm for a given situation isn’t always obvious. Sometimes you’ll want to test your hypotheses; this can be easily done by creating a simple application to measure performance, as shown below. The Optimize.vbp sample application also contains examples of several different test scenarios.

To create a performance testing application

  1. Open a new .exe project.

  2. Create a form with two command buttons: Command1 and Command2.

  3. In the Command1_Click Event add the following code:

    Private Sub Command1_Click()
        Dim dblStart As Double
        Dim dblEnd As Double
        Dim i as Long
    
        dblStart = Timer        ' Get the start time.
    
        For i = 0 To 9999
            Procedure to test    ' Enter your procedure here.
        Next
    
        dblEnd = Timer            ' Get the end time.
    
        Debug.Print dblEnd - dblStart    ' Display the
                                            ' elapsed time.
    End Sub
    
  4. Add the same code to the Command2_Click event, substituting the second version of your procedure inside the loop.

  5. Run the application and monitor the results in the Immediate window.

This example uses the default property of Visual Basic’s Timer class to time the execution of the procedure within the loop. By placing your code inside the loop for each command button, you can quickly compare the performance of two algorithms. The code can be within the loop or can be a call to other procedures.

You may need to experiment with different values for the upper bounds of the loop counter, especially for fast routines. Make sure that you run each version several times to get an average; results can vary from one run to the next.

You can also optimize your application by increasing data access speed.