article

Programming Efficient Code Version 0.9

Email
Submitted on: 1/25/2015 9:15:00 PM
By: Sachin Mehra (Delhi) (from psc cd)  
Level: Intermediate
User Rating: By 10 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access, VBA MS Excel
Views: 500
 
     Our programs spend a lot of time in loops, and unfortunately loose a lot of their performance in them as well.

 
				

Programming Efficient Code



Programming Efficient Code Version 0.9

Focusing Loops

Article by Sachin Mehra (sachinweb@hotmail.com)

 

One of the worst things that a programmer can assume is that the compiler and middleware will do the optimizations for you!  Most applications are being targeted for 50-200 concurrent users, which is why we need to constantly be worrying about the performance of our code.

 

Say you have a search component (which will be on everybody’s desktop) which takes 3-5 seconds to load; and consequently ties down the database.  Imagine what will happen when 200 people try to load this at the same time.  Simple math would suggest (say using an average of 4 seconds): (4 x 200) / 60 = 13 – THAT’S 13 MINUTES!  And actually, when dealing with situations of high contention, you cannot assume 100% efficiency and could be realistically dealing with something in the range of 20-25 minutes of processing time required. 

 

There is no ‘silver bullet’ to making fast and efficient code.  Middleware will not solve the problem for you, databases will not solve the problem for you, it is up to you as a computational process engineer (how’s that for a title?) to understand and deal with the underlying inefficiencies in the software you design.  There are many things which you need to consider, and you need to think your logic out carefully.  One thing you should always be asking yourself is “could this be done better?”.

 

 In programming, we find ourselves in loops a lot.  In VB, ASP and COM, we especially find ourselves looping through Collection objects an awful lot.  This is one of the particular areas where many of us need some improvement. 

 

Our programs spend a lot of time in loops, and unfortunately loose a lot of their performance in them as well.  I will try to cover a few pointers which may help you in certain situations save some unnecessary computational cycles off you’re code.

 

People tend to think from beginning to end, and they tend to program in this forward lineage as well.  But this can often be inefficient.  Sometimes the computer can find its way from the end to the beginning much faster. 

 

Consider the code in Example 1.

 

Example 1:

 i =0

While i <> ubound(arrayName)

                doSomething

i = i + 1

Wend

 

 

This is a fairly straight-forward while-loop to iterate that iterates through an entire array collection to do something.  But consider Example 2:

 

Example 2:

 

 i = ubound(arrayName)

While i <> 0

                doSomething

i = i - 1

Wend

 

 

This example is many times more efficient than Example 1.  In example one, we are making a call to ubound(arrayName) for every iteration through the loop which is unnecessary, and also we are doing a direct X AND comparison to determine if the loop should continue which is also more efficient.  By looping backwards through the Array we manage to increase processing efficiency but 50% or more!  

 

Final Words

Programming is all about problem solving.  And as with other kinds of problem solving, there are always many different ways to solve the problem.  However, some ways are more certainly better than others.  You should take the time to understand how the underlying components you are using actually work, and why they work they way they do.

 

Version 1.0 of this article shall be comming soon, I need my friend Romi's help to work out and try a few application tests before comming up with the final version. You may also send in your inputs to me.

 

Article By Sachin Mehra (sachinweb@hotmail.com) 

 


Other 1 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 Intermediate 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


 There are no comments on this submission.
 

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.