Important alert: (current site time 7/16/2013 12:32:38 AM EDT)
 

article

Optimizing your VB code even more

Email
Submitted on: 8/7/2001 6:22:50 AM
By: Almar Joling  
Level: Beginner
User Rating: By 54 Users
Compatibility: VB 5.0, VB 6.0
Views: 47661
author picture
(About the author)
 
     This article shows various speed increasing methods, I've done tests myself, you can see the performance difference in a graph and in a table. This article is really worth your time. For example, it shows how one simple "\" instead of "/" will speed up your app with more than 400% percent!

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				Something weird happened with the original tutorial. Probably because of the tables. 
I don't like to do this, but I really had to upload this awesome article at my website:
http://www.quadrantwars.com/optimizations.htm
Please vote if you like it. I'm sure you do!


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/7/2001 8:09:53 AMEric Dalquist

Great tips. I've wondered about the speed differences of different styles of VB but never took the time to actually do the test. Glad someone did.
(If this comment was disrespectful, please report it.)

 
8/7/2001 9:52:03 AMRick

Good Stuff!
(If this comment was disrespectful, please report it.)

 
8/7/2001 10:10:37 AMShawn Elliott

Another very important optimization technique is to reference array items and then use that.
example:
dim states(0 to 4) as string
states(0) =
(If this comment was disrespectful, please report it.)

 
8/7/2001 10:11:38 AMShawn Elliott

I'll try this again

Another very important optimization technique is to reference array items and then use that.
example:
dim states(0 to 4) as string
states(0) = "Alabama"
states(1) = "New York"
states(2) = "Louisiana"
states(3) = "Texas"
states(4) = "Illinois"

This section here :

if len(states(1)) < 5 and mid(states(1),1,1) = "A" then
'Do some freaky thing
end if


Can be better written as :

dim MyState as string
MyState = states(1)
if len(MyState) < 5 and mid(MyState,1,1) = "A" then
'Do some freaky thing
end if

The reason you get a performance gain is because you aren't doing an array lookup each time you use the variable states(1). It's worth the extra memory overhead of the new variable
(If this comment was disrespectful, please report it.)

 
8/7/2001 12:30:57 PMAlmar Joling

Shawn: You're absolutely right. I showed this already a bit with the temporary variables, but I'll add this one as well later =-)
(If this comment was disrespectful, please report it.)

 
8/7/2001 12:35:05 PM(Tim Miron) yar-interactive software

Well almar, thanks for the follow-up on my article. Hey actually your the guy who's making QW right, i made that splash screen concept remember? hows all that coming? I gota give you five globes on this, i didn't know alot of this stuff my optimization article was a fairly simple "Did you know..." type of jot-note style article... Why didn't you submit this sooner? LOL. Good Job from your "competitor" (I wrote the first optimization article but it wasn't as extensive... however i think it imspired Almar to finally post this article) FIVE GLOBES FOR YOU!

Tim

PS - get back to me sometime about me helping with QW please... timbo_m45@hotmail.com
(If this comment was disrespectful, please report it.)

 
8/7/2001 8:37:23 PMGary Staunton

Excellent info, would be nice to know when your site is back up.
(If this comment was disrespectful, please report it.)

 
8/8/2001 3:04:21 AMIgguk

Very interesting!
Maybe I can add another trick. I noticed in your examples for loops you use :
For I=0 To 10000
Next I
Though you can still improve the performance with :
For I = 0 To 10000
Next
I have never measured the gain you can expect but it's quite important
Never repeat the variable after Next, if you want your code to be easier to read, just place it as a comment...
(If this comment was disrespectful, please report it.)

 
8/8/2001 3:45:27 AMAlmar Joling

Igguk: There is no difference between
(If this comment was disrespectful, please report it.)

 
8/8/2001 5:34:03 AMMatrix Man

Almar, he means:

for i = 0 to 100000
next i

is slower than:

for i = 0 to 100000
next (note the missing i)



(If this comment was disrespectful, please report it.)

 
8/8/2001 5:47:29 AMAlmar Joling

Hmm, apperently my message got cut off. Anyway, I know what he meant, but there really is no difference in it. It gets converted to an
(If this comment was disrespectful, please report it.)

 
8/8/2001 6:13:12 AMRudy Alex Kohn

I have some more speed up tips, with string handling etc.. Posting them soon - So stay tuned. Oh, Nice artictle ;)
(If this comment was disrespectful, please report it.)

 
8/8/2001 10:03:40 AMDave

Great article! Another good tip is to try and use vbNullString instead of "".
Example: If strTest = vbNullString Then...
(If this comment was disrespectful, please report it.)

 
8/8/2001 4:43:12 PMAlmar Joling

Hmm, never thought about that one Dave... I might do some testing on that =-)
(If this comment was disrespectful, please report it.)

 
8/9/2001 11:52:42 AMdanda311

You have some nice stuff here, but there is only one problem you throw around longs like there is no tommarow. Longs may be 32 bit, but they are also the largest variable in byte size (Excluding String's cause they vary)., They are as big as variant. So you can use them, but when projects get bigger so does memory usage. I don't care ihave 384 megs of ram but the average household has 64-128. That and the systray is usually loaded also. Thats beside the point though make sure you don't overuse longs. Thank you lates.
(If this comment was disrespectful, please report it.)

 
8/9/2001 12:03:47 PMAlmar Joling

I have to agree partly... Longs are indeed taking up 4 bytes. But if you use them as temporary variable (dimmed in a sub/function), the allocated memory will be free once the sub has been processed.

But imagine... 4 bytes. Even if the average household has 64mb (since memory is currently very cheap 128-256 is more likely), you can put 256 longs in 1 kb! Now imagine how many could be filled in a mb. Although that really looks much, hmm... Might be mistaken =-)
(If this comment was disrespectful, please report it.)

 
8/9/2001 12:10:17 PMJoe

Excellent article. I am always into speeding up code. Never thought about the Left$, Mid$ or Right$.
(If this comment was disrespectful, please report it.)

 
8/9/2001 12:46:43 PMSrideep Prasad

Excellent Info ! And of course, very useful... I guess you might have spent some time researching and timing the various operations mentioned...
(If this comment was disrespectful, please report it.)

 
8/17/2001 3:44:16 PMChris

EXCELLENT! You get 5 start from me :)

However, I noticed in the section "Debug.Print" your comment that it still runs. Of course it does. It still runs the function called after the sub, because what if the function did something important? The correct way to test this would be to REMOVE THE Debug.Print AND LEAVE THE FUNCTION CALL.
(If this comment was disrespectful, please report it.)

 
8/26/2001 12:48:04 PMRob Wright

Very, very good - congrats.

(If this comment was disrespectful, please report it.)

 
12/23/2001 6:22:49 PMJohn Galanopoulos

That's the second excellent post i read from you. This should be part of the Hall of Fame. 5g. Bravo!!
(If this comment was disrespectful, please report it.)

 
6/23/2002 8:57:15 AMOzan Yasin Dogan

Nice tips, if you want to look a way on how to read / write a file as quick as possible in VB, go on to my submission at:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=34988&lng WId=1

(If this comment was disrespectful, please report it.)

 
6/23/2002 3:00:09 PMMike Christian

Very nice tutorial, Almar. I would be interested to see if there is any speed increase with the following For/Next loop implementation using your criteria:
Dim I As Long, L As Long, U As Long
L = Lbound(MyArray)
U = Ubound(MyArray)
For I = L to U
Next
Since Lbound & Ubound appear to be functions.
(If this comment was disrespectful, please report it.)

 
6/23/2002 7:41:22 PMpeter cant

Nice tips. I had a look at Strcmp before and saw in the help:
"vbTextCompare 1 Performs a textual comparison. " How helpful -- not! So thanx for explaining what it does and how it helps.
(If this comment was disrespectful, please report it.)

 
6/24/2002 9:02:20 AMGary Choma

Dave, instead of using strTest = vbNullString, use Len(strTest) = 0. It's faster because the string value never get's evaluated.

Another thing to keep in mind with optimization: In MOST cases, the savings are negligible (unless extensive looping occurs). It's usually better to write code that's easily understood by another programmer rather than writing something cryptic that executes .2 seconds faster! And when writing code using optimization tricks, it's a good idea to explain what you're doing in comments! Like if I saw "X \ 1" instead of "Cint(x)", it might leave me wondering what's the purpose if it wasn't explained in a comment.

Good stuff to know, in any case!

(If this comment was disrespectful, please report it.)

 
6/24/2002 10:51:49 AMChoCho

Excellent! I have always wondered about all of that!

Thanks!
(If this comment was disrespectful, please report it.)

 
6/24/2002 10:53:57 AMAlmar Joling

I'm not sure why this article attracts so many new people the last few days (I'm the author), but just wanted to say I've updated my original site a LONG time ago, check http://vbfibre.digitalrice.com.

Thanks!,
Almar
(If this comment was disrespectful, please report it.)

 
6/27/2002 7:27:28 AMD.

These are useful tips could be useful to anyone, regardless of level of expertise. Thanks
(If this comment was disrespectful, please report it.)

 
7/28/2002 9:56:41 PMAllan

Can't find the article on your site!!!
(If this comment was disrespectful, please report it.)

 
10/7/2002 1:21:36 AMSmartCoder1

Excellent Article! Just out of curiousity how did u get those figures exactly in seconds ?
(If this comment was disrespectful, please report it.)

 
12/20/2002 1:01:10 PMMark2

very nicely done, thanks for the tip!
(If this comment was disrespectful, please report it.)

 
1/4/2003 9:50:02 PM

Cool! good work = 5 globes/stars/circles/ect
(If this comment was disrespectful, please report it.)

 
6/24/2003 4:04:32 AMmagedsoft

Excellent article.
just a question, when you compaired with vs no with, did you have say 10 forms in the project so with may avoide searching through a list or something, I am not the best in programming but just occured to my mind
(If this comment was disrespectful, please report it.)

 
4/14/2004 11:56:20 PMFrancis Arnold G. Balatico

Thanks for the testings you made. Real help. Could you recommend a software I could use for personal testing. I've always wanted to test every aspect of my application but can't find the right tool for it. If you please, email me at Crimson_Zenith@yahoo.com. Thanks!
(If this comment was disrespectful, please report it.)

 
10/15/2004 3:34:02 AM

Good
(If this comment was disrespectful, please report it.)

 
10/18/2005 8:24:30 AMZaid

Ehmmm, i'm very glad to read all you're artycel..
(If this comment was disrespectful, please report it.)

 
11/2/2005 2:52:08 AMfiruz

good work.people can learn something new under simulation and animation

(If this comment was disrespectful, please report it.)

 
11/8/2005 10:37:52 PMncmit

good article !!! thanks alot
(If this comment was disrespectful, please report it.)

 
7/7/2006 3:28:09 PM...

Excellent article, it was very clear and concise.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.