Here
are some tips for optimizing your code mainly for speed.
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.
'Ex.1
For
X = 1 to 10000
A = X * 3.1415927
Next X
'Ex.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.
'Ex.
1
For
X = 1 to 1000
For Y = 1 to 100
P = X * 10 + Y
Next Y
Next X
'Ex.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.
Use
loops properly
Just
take a look at the following to examples and you'll understand
what I mean.
'Ex.1
For
X = 1 to 10000
For Y = 1 to 10
P = X + Y
Next Y
Next X
'Ex.2
For
Y = 1 to 10
For X = 1 to 10000
P = X + Y
Next X
Next Y
Count
the times each line is executed and the results are simple:
Line
No
|
Times in Ex. 1
|
Times in Ex. 2
|
1
|
1
|
1
|
2
|
10000
|
10
|
3
|
100000
|
100000
|
4
|
100000
|
100000
|
5
|
10000
|
10
|
All
|
120001
|
100001
|
Load
most used forms in memory
Loading
a form may cost a lot of time in heavy situations. Thus, you should
load your most used forms when your application is not busy doing
other things. A good idea is using a splash screen and loading
your forms when your program is started. You can also do it when
your program is being minimized, or has lost focus.
API
vs. VB
In
some cases, using API may be faster than calling VB functions.
For example, retrieving a directory structure using API is about
four or five times faster than using the standard VB functions.
When working with graphics, you cannot miss to call an API. But
not all the VBs are slow. You must experiment to find what does
better. Just save
Timer in a variable, make
a loop calling a VB function and at the end display the time elapsed.
Do the same with the API and make up your mind what will be worthy.
Big
variables - ByRef rather than ByVal
When
a variable is passed to a procedure ByVal, this mean it won't
and cannot be changed by this procedure. If the procedure changes
this variable, it does it for itself only, the original value
is never affected. And here is how VB makes it - It just copies
the variable to a new location in memory and gives this location
to the procedure. But everyone knows that copying memory takes
time. Now lets see what happens when a variable is passed ByRef.
It's very simple, if the variable can be changed by the procedure,
then it is not necessary that the procedure will be given a copy
of this variable. VB just gives the procedure the original location
of the variable. It just saves the time for creating a copy of
this variable. So, when using big variables (mostly user-defined)
pass them ByRef to safe some precious time.
Procedure
calls loses time
It's
true. When calling procedure or function a bit of time is wasted.
It's just a little bit, but if your procedure consists of several
lines and in your app code you call it 5000 time in a loop or
something, then you should just copy this several lines in the
loop to avoid losing much time. Your program may lose some readability,
but it will gain speed.
|