3/30/2007

If (Not Me.efficientProgrammer) then me.goodProgrammer = false

Here at Digi-Plex Industries, our systems are state of the art. However, no matter how fast their processors may be nor how much memory they may have, they are constantly running several thousand programs at once. Therefore, if one of our programmers is dubbed inefficient, we fire them immediately…

Okay so most companies are not that harsh, and this method (reminiscent of Despair, Inc.’s method of fixing their morale problem) won’t fix much. However, even though companies usually don’t fire their employees for inefficient coding, they won’t hire you again if your code weighs down their system after they hired you independently to make their scheduling system. So to keep your code from bringing their system to a standstill, here’s some tips on how to keep your code efficient.

1. Limit the If statements
If you have several dozen “if” statements and if you have them all one after the other and if they all test the same thing and if they all must be satisfied and if… well, you get it. “If statements” are tests on a certain variable or group of variables. For every if statement in the code, the program must go over and check to see if the variable tests correctly. If there are a string of if statements, then the code takes longer to execute. To correct this problem, try these steps:

1. Find an algorithm to do what you want. Look at the title of this post. It could be made more efficient by instead typing:

me.goodProgrammer = me.efficientProgrammer

2. If there is no algorithm, then see if you can put them into an “if… else if” chain.

If key.isDown(“Up”) then
    Me.Move(0,5);
Else if key.isDown(“Down”) then
    Me.Move(0,-5);
End if

2. Remove unnecessary Print statements
If there is anything that is printing every frame, every second, or every loop, remove it – immediately. These print statements, which should be used for debugging and debugging alone, slow down the computer drastically as it prints and updates the screen. If you need something to give a status report, do it only every so often, such as every minute or 15 seconds, but not constantly

3. Give it a break every so often in a loop
In the better programming languages, such as Visual Basic, it has a function that hands processing power off to the system for a moment before ceasing it again for its process. In Visual Basic, this function is called DoEvents. The function allows other processes to run besides itself and prevents the computer from thinking that your program has become unresponsive even though all its doing is parsing a 23MB text file letter by letter (by the way, don’t do that). DoEvents also allows the Windows GUI (Graphics User Interface, aka, the windows on the screen) to update the program’s window. So if your progress bar was not updating and you don’t know why, try inserting DoEvents in there.

4. Do Not Work from the Disk!
Do NOT Work From the Disk! File I/O is the slowest thing on a computer system. The last thing you want to do is save every variable that you change to the disk as you do the process. That was what memory was invented for; if possible, pull the file into memory and work on it there. Now some things, such as gigantic databases, are not possible to pull into memory and therefore, you have to work on it from the disk. In these situations, you need a different method. I will outline that different method when I learn about it, but for now, read number 5.

5. When searching a database, binary is your friend.
If you need to search for something in the gigantic database mentioned above, Do Not For Loop Your Way Through It. Use what is called a binary search: First, find the midpoint in the data. Second, is the data you want before or after that midpoint? Third, cut the data you are looking through in half and do the First, Second, and Third steps again with that data. This method is much faster; I would vouch to say a good 15-20 times faster.

There are more tips to come; I write them down as I think of them. But these first five will get you off to a pretty good start. Start applying them to all your code and you will have a much faster program before you know it.

I just hope I’m not blabbing on into space for no reason…

No comments: