Wednesday, June 10, 2009

Optimization: Avoid Redundancy

Programmers are often seeking methods to improve the performance of their code; indeed, code optimization has become a spectator sport amongst programmers. While optimization is a very complex topic, visual basic programmers can write faster code by following a few tips on code redundancy. Since the elimination of code redundancy can greatly improve the performance of software, visual basic programmers should proof read their software for code redundancy during the optimization process.

Redundant code inside of loops can be very taxing on software performance. One unnecessary instruction inside of a loop can have a large impact on the performance of software because it is repeated with every iteration of the loop; therefore, programmers should carefully check loops for unnecessary instructions. Visual basic programmers should also avoid accessing the properties of objects inside of a loop; instead, they should cache the needed properties inside of variables. In addition to properties, programmers should avoid function calls inside of loops because function calls are expensive operations. Loops are the most critical part of software, and programmers should pay careful attention to avoid redundant code in them.

For example, the following code has an instruction that never changes; as a result, the software will repeat the useless instruction fifty times.

for i = 1 to 50
x = b ' Stays the same with every loop, get it outside of the loop!
k = j + I
j = j + 1
next i


The above code should be replaced with the following to avoid redundancy.

x = b 'Moved outside the loop

for i = 1 to 50
k = j + I
j = j + 1
next i


In another example, the following code accesses a property inside of a loop. Every single dot corresponds to one function call.

for i = 1 to 50
text1.text = text1.text + b(i) '100 function calls.
next i


In the above example, two function calls are made with every iteration of the loop. To avoid the function calls, the data can be cached in a variable then assigned to the property after the loop is finished.

for i = 1 to 50
strbuffer = strbuffer + b(i) ' No function calls.
next i
text1.text = strbuffer


Finally, the following example illustrates a redundant function call. Many visual basic programmers use built in functions carelessly inside of loops, but it should be avoided for performance reasons.

for i = 1 to 100
sz = len(string) ' One hundred function calls
'Do processing
next i


The above code can be simplified to the following correction.

sz = len(string) ' One function call
for i = 1 to 100
'Do Processing with sz
next i


Programmers should always search for unnecessary calculations and poorly designed decision nests in their code. Unnecessary calculations are a frequent problem in code because many programmers are not mathematically strong. Many programmers include additional instructions to acquire a result that can be obtained with fewer calculations. Aside from calculations, programmers should be careful when they design decision nests in code. The conditional branch that is most likely to be true should be placed the highest in a decision nest, and the conditional branch that is most likely to be false should be placed the lowest in the nest. If programmers pay careful attention, they can avoid unnecessary calculations and design better decision branches.

In the following example, the calculations being made are redundant because parts of the calculation can be shared. The variables a and b are multiplied twice.

x = a * b + c
y = a * b + d


To avoid redundancy, programmers can simply share parts of the calculations.

t = a * b
x = t + c
y = t + d


Programmers should avoid redundant memory allocation as much as possible. Memory allocation is a very expensive process to perform in code, and it is frequently a topic in optimization. Like hard drives, frequent memory allocation and freeing of small data may cause memory fragmentation. After computer memory becomes fragmented, computer performance is severely effected because the memory manger must find appropriate memory locations that are large enough to store data. When programmers need to allocate memory, they should attempt to allocate enough memory to avoid the need for reallocation.

In conclusion, visual basic programmers can speed up their applications by reducing code redundancy. Code redundancy is a common occurrence in visual basic programs, and it can be avoided with careful attention to detail.

No comments:

Post a Comment