How to shave off those few milliseconds...

Show Index

If your program is running sluggish, then here are some killer tips to improve the efficiency of your code.
Copy and paste the example code snippets in the right column to see what kind of a difference these tips make on your machine.

Note that the examples given are not necessarilly real world examples.  The examples are condensed down to the bare minimum code necessary in order to be easily understandable and some examples use what would usually be unrealisticly large loops just in order to show performance differences.  But they are all valid optimizations and when used together can get you a little more processing power out of your programs.
Also, the speed improvements listed (i.e. 30% faster) are approximations and vary from computer to computer.
These examples were run on a 2.5GHz processor and depending on your computers speed may be extremely slow...

 

The BEST improvements

Need to squeeze a little more?

STILL NEED MORE? /now showing/

 

STILL NEED MORE?  In a pinch, you can try these optimizations:
PLEASE NOTE: These optimizations are usually only useful when used in large quantities, such as in very large loops.  They make VERY small improvements to your code execution time.  HOWEVER, if you have a few sections of your code where a very large quantity of computations/loops are being performed, then these optimizations may squeeze just a little more out of those sections where you really need it.

Gosub ~70% faster than functions

If you really need to, you can convert some of your Functions into Gosub routines.  Functions require a small amount of overhead.   They have to push their called parameters onto a stack and pop them off when they return.  Gosubs don't require as much overhead and therefore can give you a speed advantage.

;use a Function (slower)
start = MilliSecs()
For n = 0 To 100000000
	myfunc()
Next
Print MilliSecs() - start

;use a Gosub (faster)
start = MilliSecs()
For n = 0 To 100000000
	Gosub mylabel
Next
Print MilliSecs() - start

WaitKey
End

Function myfunc()
	a=a+1
End Function

.mylabel
	a=a+1
Return
Goto

Goto is FAST.  It's the quickest way to get from one point in your program to another.   Sometimes, if you really need it, you can shave off a little time, by UNstructuring your code, and using a Goto to instantly skip from point A to point B.

The example provided isn't the best but does the job of showing how useful Goto can be.  In the example, the variables c, d, e, f, and g are only incremented if dontDoIt is False.  dontDoIt never equals False in this example so the variables are never incremented, BUT the computer still has to execute each If statement associated with each variable (If Not dontDoIt Then ...) as it exits each nested If a <> True statement.

In the second timed loop however,  the program uses the Goto to just jump immediately out of all of the nested Ifs at once (since there really is no reason to bother checking any of the c, d, e, f, or g variables) and saves some processing time.

Many programmers will tell you that using Goto is bad programming practice and produces 'spaghetti' code.  This can be very true but if you really need to tweak a certain section of your code, and you've tried other optimizations but you still need more, then this might be a solution that works, especially when you need to jump out of a number of nested Ifs.

start = MilliSecs()
For n = 0 To 100000000
	dontDoIt = False
	If a <> True
		If a <> True
			If a <> True
				If a <> True
					If a <> True
						If a<>True Then dontDoIt=True
						If Not dontDoIt Then c = c +1
					EndIf
					If Not dontDoIt Then d = d + 1
				EndIf
				If Not dontDoIt Then e = e + 1
			EndIf
			If Not dontDoIt Then f = f + 1
		EndIf
		If Not dontDoIt Then g = g + 1
	EndIf
Next
Print MilliSecs() - start

start = MilliSecs()
For n = 0 To 100000000
	If a <> True
		If a <> True
			If a <> True
				If a <> True
					If a <> True
						If a <> True Then Goto label
						c = c + 1
					EndIf
					d = d + 1
				EndIf
				e = e + 1
			EndIf
			f = f + 1
		EndIf
		g = g + 1
	EndIf
	.label
Next
Print MilliSecs() - start

WaitKey
 

 

If you've reached this page and there's no index on the left, Click here to show the Index Page