Ads Top

Increasing Speed in VB

Below are guidelines to help increase speed, available resources, available RAM, and available disk space in Visual Basic:

INCREASING SPEED
You can increase speed in the following ways:
1. Preload forms.
2. Store graphics as bitmaps.
3. Place debug routines in a separate module.
4. Use dynamic-link library (DLL) routines.
5. Use integer and long variables instead of single, double, or currency variables.
6. Cache frequently accessed properties in variables.

Example #1: Demonstrating Integers Versus Single Variables in a Loop
Start Visual Basic; or choose New Project from the File menu (ALT+ F, N) if Visual Basic is already running. Form1 is created by default.
Add two command buttons and two labels to Form1.FRM.
Add the following two procedures to the (general) section of Form1.

Sub Ints ()
Dim i As Integer
i = 1
Do Until i >= 5000 ' = 5000 iterations
label1.Caption = Str$(i)
i = i + 1
DoEvents
Loop
End Sub

Sub Reals ()
Dim r As Single
r = 1#
Do Until r <= 500 ' = 5000 iterations
label2.Caption = Str$(r)
r = r + .1
DoEvents
Loop
End Sub

Add the following code in the Command1_Click event procedure:
Sub Command1_Click ()
StartTime = Timer
Call Ints
EndTime = Timer
MsgBox "Time for Integer Loop was: " & CStr(EndTime - StartTime)
End Sub

Add the following code in the Command2_Click event procedure:
Sub Command2_Click ()
StartTime = Timer
Call Reals
EndTime = Timer
MsgBox "Time for Real Loop was: " & CStr(EndTime - StartTime)
End Sub


Run the sample. First test how long it takes the integer loop to process, then test how long it takes the single loop to process. You should see that the integer loop runs faster.

Find More Optimized VB
Tutorial by Saqib Sajjad
www.justvb.net

No comments:

Powered by Blogger.