Improve Performance by Eliminating Subexpression

Arrays can reduce the amount of code you must write to perform an operation, especially when used with a loop. By using the same array element in multiple places in your code, you can increase the speed with which your code executes if you resolve the array only once. This code determines if the value of the array element is this value, or if the value of the array element is that value:

 If intArray(intArrayIndex) = 5 or _	intArray(intArrayIndex) = 10 Then

Because Visual Basic must calculate the location of the array element to resolve its value (base + intArrayIndex * sizeof_intArray_type), it makes sense that the code executes faster the less the calculation is performed. This code provides a faster implementation:

 TheValue = intArray(intArrayIndex)If TheValue = 5 or TheValue = 10 Then

This technique offers performance gains that are even more dramatic when used in a loop:

 For intArrayIndex = 0 to 10TheValue = intArray(intArrayIndex)If TheValue = 5 or TheValue = 10 Then	' Do something hereEnd IfNext

By eliminating the redundant subexpression and assigning the value to the variable named “TheValue” only once per loop (executes 10 times) instead of twice per loop (executes 20 times), you significantly reduce the amount of work that VB must perform to complete this loop.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS