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
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
Routine to test ' Enter your routine here.
Next
dblEnd = Timer ' Get the end time.
Debug.Print dblEnd - dblStart ' Display the
' elapsed time.
End Sub
This example uses the default property of Visual Basic’s Timer class to time the execution of the routine 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.