The Optimization Process - danielep71/VBA-PERFORMANCE GitHub Wiki

[Home]] ](/danielep71/VBA-PERFORMANCE/wiki/[[Previous|Home) | Next


3. The optimization process

3.1. Measure performance

To measure code performance, I have prepared a dedicated class here.

3.2. Disable time wasters

To manage time waster, I have prepared a dedicated class here.

3.3. Restructure (macro-optimization)

Still slow? Restructure your code.

Macro-optimization looks at the structure of the procedure to ensure it uses the most efficient algorithms and minimizes the amount of code that needs to be executed. This is where most times savings are usually found.

Removing the bottlenecks could mean finding a different and faster way to perform the same task. Either by rethinking the entire approach, so the task that took so long is no longer needed, or by figuring out how to accomplish this task in less time.

Sometimes instead of analyzing the problem and identifying a solution, we must use our imagination to find a different and better way to perform the same task. In short, it means concentrating on optimizing the procedure by changing how things are done to reduce the amount of processing required.

The slowest parts of the procedure invariably involve either:

  • external data retrieval
  • repeatedly loops through data sets. Larger loops are almost always an opportunity for optimization. An improvement that can be made inside the loop, however minor, is again many times over.

3.4. Micro-optimization

Need improvements? Use micro-optimization and coding best practices.

Micro-optimization ensures that the most efficient VBA statements and data types are used within the code.

This accounts for the final few percentage points and usually only has an impact where loops are executed thousands of times.

Both in pure VBA and when automating Excel, there are usually a number of alternative ways of doing the same thing, some faster than others.

These micro-level optimizations require a good understanding of the tool being used (that is, VBA and/or Excel), are often counter-intuitive, and are often different for different data types (Longs versus Strings versus Variants).

The trade-off is complexity and code readability.

For micro-optimization, I have prepared a selection of tips and tricks or coding "best practices" here.

3.5. Improve the user experience

Can't you do better? Improve the user experience.

Simply improving the few slowest procedures often makes the whole application more responsive overall.

  • Insert a splash screen when your application starts. Splash screens are typically to notify the user that the program is in the process of loading. They provide feedback that a lengthy process is underway. Occasionally, a progress bar within the splash screen indicates the loading progress. A splash screen disappears when the application's main window appears. Splash screens typically serve to enhance the look and feel of an application hence they are often visually appealing. They may also have animations, graphics, and sound.
  • If a procedure takes more than about a second, change the cursor to an hourglass at the start of it and back to normal at the end. This tricks the user into expecting a delay, and they are pleasantly surprised when it finishes quickly.
  • If the procedure takes more than about 5 seconds, display a progress bar on the screen or on the status bar. A progress bar that quickly reaches 100% gives the impression of speed. In contrast, a progress bar for a lengthy procedure lets users know the procedure is advancing, estimates how long it is left, and gives them something to concentrate on so they don’t think the application is frozen and get the urge to press Ctrl+Alt+Del.

[Home]] ](/danielep71/VBA-PERFORMANCE/wiki/[[Previous|Home) | Next