uuh algorithm - how to make a fast code

Blitz3D Forums/Blitz3D Beginners Area/uuh algorithm - how to make a fast code

GC-Martijn(Posted 2004) [#1]
Sorry for the strange title and code below.

How can I make a fast routine ?
Now I have something like this:

function getIt(var1%,var2%)

if var1% = a And var2% = b then var3% = c
if var1% = a And var2% = b then var3% = c
if var1% = a And var2% = b then var3% = c
if var1% = a And var2% = b then var3% = c
if var1% = a And var2% = b then var3% = c
; in the 10000 lines

return var3%
End function


How can I speed this up ?
-Must i make one big file.bb and include that ?
-Must i make a big file.dat and read that ?
-Must i use something else then If / And ?
-Maybe other ideas in the same concept

Thanks,
GC-Martijn


Warren(Posted 2004) [#2]
What are you trying to accomplish? It's hard to give advice when I don't know what the goal is.

As a rough guess, I would advise putting the most common situations at the top of the IF stack. That way the computer doesn't have to evaluate rare conditions every time you call the function in order to get to the common ones.


GC-Martijn(Posted 2004) [#3]
mmm

I saw a chess code that checks every player move with more then 1000 if and conditions.

And i'm wondering if there are other things that maybe faster to handle a player move with a if/and condition.

but then with this:
if var1% = a And var2% = b then var3% = c



mm damn its hard to explane [with my english] :S
forget the question...


Warren(Posted 2004) [#4]
If you have to use a stack of if statements, the only real optimization you can use is what I proposed : put the most common situations near the top so you don't have to evaluate a stack of rarely seen conditions every function call.


GC-Martijn(Posted 2004) [#5]
:) o yea thanx for that , that's a(n) optimization


Andy(Posted 2004) [#6]
Do you really need to evaluate every pair of var1% and var2% every time the fuction is called?

How often do you call the function?

Andy


Perturbatio(Posted 2004) [#7]
Make sure you use ElseIf for subsequent If statements otherwise all if's will presumably still be evaluated.

function getIt(var1%,var2%)

if var1% = a And var2% = b then 
   var3% = c
elseif var1% = a And var2% = b then 
   var3% = c
elseif var1% = a And var2% = b then 
   var3% = c
elseif var1% = a And var2% = b then 
   var3% = c
elseif var1% = a And var2% = b then var3% = c
; in the 10000 lines

return var3%
End function


*EDIT*

Alternatively:
Function getIt(var1%,var2%)


Select True
	Case var1% = a And var2% = b
		var3%=c
	Case var1% = a And var2% = b
		var3%=c
	Case var1% = a And var2% = b
		var3%=c
	Case var1% = a And var2% = b
		var3%=c
	Case var1% = a And var2% = b
		var3%=c
End Select

Return var3%
End Function



wizzlefish(Posted 2004) [#8]
Would a For...Next loop work for this?


QuietBloke(Posted 2004) [#9]
if Im understanding your code right then why dont you create a 2d array
dim c%(a,b)
populate the array with what you want the value of c% to be for each value of a% and b%.

then your code will just be :

var3% = c( var1%, var2% )

did that make sense ?