# Optimize your code: Size & Speed

Here are some tips for optimizing your code mainly for speed.

I found some tips by Billy&George Software and Peter Dimitrov, if you want to get some more, search VB Online for "Optimizing for speed", "Optimizing code" and "Optimizing for size".

Take a minute to think over these pieces and you'll find that your code can be made much faster.

Use variables instead of constants

If you are using given constant many times in your code, then why don't you store it in a variable and use the variable later. See the following two extracts of code. The second one executes faster.

'Executes 1

For X = 1 to 10000

A = X * 3.1415927

Next X

'Executes 2

PI = 3.1415927

For X = 1 to 10000

A = X * PI

Next X

Use simpler math operations

Different types of calculation take different time for the computer to process. Thus wherever you can, replace expressions like X * 2 with X + X, A / 10 with A * 0.1 or A^2 with A * A. They really run faster.

Be careful with repeating operations

Sometimes, a certain calculation may be done several times in vain. This could happen in many different ways. I will illustrate it with two simple examples.

'Executes 1

For X = 1 to 1000

For Y = 1 to 100

P = X * 10 + Y

Next Y

Next X

'Executes 2

For X = 1 to 1000

T = X * 10

For Y = 1 to 100

P = T + Y

Next Y

Next X

The second example runs better, because X * 10 is repeated only 1000 times compared to 100000 times in the first one.

Read to Microsoft's article on How to Optimize Size and Speed of Visual Basic Applications

I found some tips by Billy&George Software and Peter Dimitrov, if you want to get some more, search VB Online for "Optimizing for speed", "Optimizing code" and "Optimizing for size".

Take a minute to think over these pieces and you'll find that your code can be made much faster.

Use variables instead of constants

If you are using given constant many times in your code, then why don't you store it in a variable and use the variable later. See the following two extracts of code. The second one executes faster.

'Executes 1

For X = 1 to 10000

A = X * 3.1415927

Next X

'Executes 2

PI = 3.1415927

For X = 1 to 10000

A = X * PI

Next X

Use simpler math operations

Different types of calculation take different time for the computer to process. Thus wherever you can, replace expressions like X * 2 with X + X, A / 10 with A * 0.1 or A^2 with A * A. They really run faster.

Be careful with repeating operations

Sometimes, a certain calculation may be done several times in vain. This could happen in many different ways. I will illustrate it with two simple examples.

'Executes 1

For X = 1 to 1000

For Y = 1 to 100

P = X * 10 + Y

Next Y

Next X

'Executes 2

For X = 1 to 1000

T = X * 10

For Y = 1 to 100

P = T + Y

Next Y

Next X

The second example runs better, because X * 10 is repeated only 1000 times compared to 100000 times in the first one.

Read to Microsoft's article on How to Optimize Size and Speed of Visual Basic Applications

## No comments:

Post a Comment