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? /now showing/

STILL NEED MORE?

 

Need to squeeze a little more?...

Use Integers whenever possible
~10x faster than floats

Computers can process integer variables the fastest of all variable types.  If a variable in your code doesn't require it to be a float# or a string$ or a custom type, then make sure it's an integer%.

 

Use Floats when performing real math
~30% faster than using integers

An exception to that rule though is to use floats if you are working with floats so that the computer doesn't have to perform any float to integer conversion.

;use integer, add float (slowest)
start = MilliSecs()
For n = 0 To 10000000
	b = b + 1.6
Next
Print MilliSecs() - start

;use float, add float (faster)
start = MilliSecs()
For n = 0 To 10000000
	a# = a# + 1.6
Next
Print MilliSecs() - start

;use integer, add integer (fastest)
start = MilliSecs()
For n = 0 To 10000000
	b = b + 1
Next
Print MilliSecs() - start

WaitKey
End
Splitting up arguments

When evaluating multiple arguments, try to set up your conditional statements so that the fastest arguments will be evaluated first.

In the example on the right, the argument y = Sqr(10000)^3.0 is extremely processor intensive.  We want to avoid evaluating that argument if possible.

In the first example, the y = Sqr(10000)^3.0 argument is ONLY evaluated if the first argument x = 1 is true.   Since, the first argument will sometimes be false, the y = Sqr(10000)^3.0 argument will not be evaluated at those times (thus saving processing time).

Compare this to the second example, where the x = 1 and y = Sqr(10000)^3.0 arguments are evaluated EVERY time because they both appear within the same If statement.

In this example, the first method is almost 100 times faster!

y = 1000000
;example 1 (faster)
SeedRnd 1
start = MilliSecs()
For n = 0 To 10000000
	x = x + 1
	If x = 500
		If y = Sqr(10000)^3.0
			x = Rand(0, 1)
		EndIf
	EndIf
Next
Print MilliSecs() - start

;example 2 (slower)
SeedRnd 1
start = MilliSecs()
For n = 0 To 10000000
	x = x + 1
	If x = 500 And y = Sqr(10000)^3.0
		x = Rand(0, 1)
	EndIf
Next
Print MilliSecs() - start

WaitKey
End
Exit

The Exit command causes your program to leave the current loop that it is in.  This can save lots of time depending on the situation.

Note that in this example, the loop that contains the Exit command exits almost immediately because the type instance that contains "it" happens to be the 500th instance in the list.  The performance gain would not be as great if the "it" type instance was the 999,500th instance in the list.   Try changing n = 500 to n = 999500.

So, as you can see, the use of this command is beneficial but like in this example doesn't guarantee any specific performance increase.

Type mytype
	Field name$
End Type

For n = 0 To 1000000
	thistype.mytype = New mytype
	thistype\name$ = "abcdefg"
	If n = 500 Then thistype\name$ = "it"
Next

start = MilliSecs()
	For thistype = Each mytype
		If thistype\name$ = "it"
		EndIf
	Next
Print MilliSecs() - start

start = MilliSecs()
	For thistype = Each mytype
		If thistype\name$ = "it"
			Exit
		EndIf
	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