Test the Comparative Benefits of Various Optimization Techniques

Problem

Now that you’ve tried the optimization techniques in this chapter, you’d like to test some additional optimization ideas. How can you test various VBA optimization techniques in a standardized fashion?

Solution

By using a Windows API call, some simple math, and a wrapper function, you can easily compare the performance of two optimization techniques with relatively high accuracy. This solution shows you how to create a form to compare the performance of two functions. It runs the functions and then displays how long each took to execute.

Open and run frmTestOptimize from 08-06.MDB. The form shown in Figure 8-15 allows you to enter the names of two functions and test their performance relative to each other. The 08-06.MDB database contains two sample functions that show the relative performance of integer division and floating-point division. (This optimization was discussed in Section 8.4.2 .) To run the test, enter:

FloatDivision(  )

into the Function 1 text box, and enter:

IntegerDivision(  )

into the Function 2 text box. Press the Test button. The form will run each function, show the time taken by each function, and tell you which function is faster and by how much.

The Test Optimizations form

Figure 8-15. The Test Optimizations form

To use frmTestOptimize to test your own functions, follow these steps:

  1. Import frmTestOptimize from 08-06.MDB ...

Get Access Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.