0

VB6 Myths

Posted by Erik Gaius,aka [ K i r E ] on Tuesday, March 10, 2009, 1:32 PM in
Warning: Some code shown in this post should NOT be used.

I learned visual basic 6 during my junior year in high school, and it was really fun compared to c++. However, I've found out that visual basic is hell of a lot slower than C, so I searched for ways to improve my application's speed. Some tricks worked, but most didn't. Here's a list of myths I discovered from then till now:

1.) Variables and when to initialize them. This code:
For i = LBound(myArray) To UBound(myArray)
Total = Total + myArray(i)
Next i
Is as fast as this code:
Dim LB As Long, UB As Long
LB = LBound(myArray)
UB = UBound(myArray)
For i = LB To UB
Total = Total + myArray(i)
Next
In fact, when compiled, they look exactly the same. Also, omitting the counter variable at the end of the for..next loop doesn't help anybody (It even reduces the code's readablity)

2.) One-liner Conditional Statements.
If condition Then statement 
Is as fast as this:
If condition Then
statement
End If
All the first example did is reduce your code's readability, nothing more.

3.) More one-liners
Dim newObject As New MyClass 
..is Actually slower than this..
Dim newObject As MyClass
Set newObject = New MyClass
due to 'auto-instatiation' used by the VB6 compiler. Every time you use the one-liner, VB will check if it should be instantiated.

4.) Bang (!) Operator, or the operator used by adodb (database objects) to simplify data access, is actually slower than fleshing the whole code out. Just like the example above, smaller code doesn't mean faster execution time.

I guess that's that for today's tips & tricks! See ya tommorow!

0 Comments

Post a Comment

Copyright © Gains & Hart Co. 2009 .Net.ph All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.