Select

Blitz3D Forums/Blitz3D Beginners Area/Select

_PJ_(Posted 2003) [#1]
Can somone please explain the point of Select/Case? I don't mean that the way it sounds, only I come from a background of earlier Basic programming and there doesn't appear to be a real advantage of select/case over If statements.

As an example, here is the code from the online docs, and beneath is how I would approach the same thing.

ONLINE CODE:
mission=Rnd(1,10)

; Start the selection process based on the value of 'mission' variable
Select mission

; Is mission = 1?
Case 1
Print "Your mission is to get the plutonium and get out alive!"

; Is mission = 2?
Case 2
Print "Your mission is to destroy all enemies!"

; Is mission = 3?
Case 3 
Print "Your mission is to steal the enemy building plans!"

; What do do if none of the cases match the value of mission
Default 
Print "Missions 4-10 are not available yet!"

; End the selection process
End Select



My Basic Code:

mission=Rnd(1,10)

If mission=1
Print "Your mission is to get the plutonium and get out alive!"
endif

If mission=2
Print "Your mission is to destroy all enemies!"
endif

if mission=3
Print "Your mission is to steal the enemy building plans!"
endif

if mission>3
Print "Missions 4-10 are not available yet!"
endif



big10p(Posted 2003) [#2]
Select is a lot neater, for starters, especially when you have many 'cases'. Also, in your If version, ALL the If statements are executed every time, even if mission=1 and the first block is executed the program continues to check if mission=2 or 3 or >3. If a Case is matched in a Select statement, the prog executes the Case code then drops out to after the End Select - i.e. doesn't bother checking any other Cases.

You can also group Cases together that need a common piece of code, e.g. Case 1,2,4,6,8. You can also define a 'Default' case to be executed if none of the other cases are matched. Hope that made sense!


soja(Posted 2003) [#3]
A little re-write of your code...
mission=Rnd(1,10)

If mission=1
	Print "Your mission is to get the plutonium and get out alive!"
ElseIf mission=2
	Print "Your mission is to destroy all enemies!"
ElseIf mission=3
	Print "Your mission is to steal the enemy building plans!"
Else
	Print "Missions 4-10 are not available yet!"
EndIf

...is effectively equal to the sample case code. Yet its main advantage is being neater. You can do everything with IFs, ELSEs, amd ELSEIFs that you can with SELECTs and CASEs, but I suppose in the cases where you've got lots of different options, a Select statement would make more sense.


gellyware(Posted 2003) [#4]
big10P, My question would be this. If the select case is at the bottom of the cases most of the time, it would still look through all of them like if statements though?

mission=Rnd(1,10); MISSION RANDOMS 4-10 EVERY TIME

; Start the selection process based on the value of 'mission' variable
Select mission

; Is mission = 1?
Case 1
Print "Your mission is to get the plutonium and get out alive!"

; Is mission = 2?
Case 2
Print "Your mission is to destroy all enemies!"

; Is mission = 3?
Case 3
Print "Your mission is to steal the enemy building plans!"

; What do do if none of the cases match the value of mission
Default
Print "Missions 4-10 are not available yet!"

; End the selection process
End Select


dynaman(Posted 2003) [#5]
Correct - which is why you should try to code it in the order in which you think it will be most efficient.

The first case should be the most frequent occurance, the second the second most, etc...

At worst it will be no worse then multiple if..then statements.


_PJ_(Posted 2003) [#6]
Interesting. Thanks - Does this really have a significant impact on speed, though? Considering 1GHz and above processor speeds.

I have also just noticed the Default command which would be a little longer to code in If/Thens


gellyware(Posted 2003) [#7]
Hi Malice. I decided to write a program to test the two of these. Now if this is in error, please let me know since im not an expert. However, it seems that IF statements ARE in fact way faster than select case. Here is my code checking for 5000000 if's against select case :)

Graphics 800, 600

number = Input ("Press Enter to check speed of IF statements ")

begin# = MilliSecs()
Print "Start Time: " + begin#

For x = 1 To 5000000
If number = x Then Print "if check"

Next

If number = 0 Then endCheck# = MilliSecs()

result# = (endCheck# - begin#)

Print "End Time: " + endCheck#
Print "Result: " + result#
Print
Print

number = Input ("Press Enter to check speed of Select case statements ")

begin# = MilliSecs()
Print "Start Time: " + begin#

For x = 1 To 5000000
Select number
Case x
Print "select check"

Default
endCheck# = MilliSecs()
End Select
Next


result# = (endCheck# - begin#)

Print "End Time: " + endCheck#
Print "Result: " + result#

WaitKey


gellyware(Posted 2003) [#8]
Wow interesting. Found a problem with my own code. The default takes time to default everytime for 5million selects, so I changed the default to nothing and they are pretty close, but IF statements still win by a tiny tiny fraction - takes 0.059 seconds longer hehe. Ok here is the correct version. By the way its good to note that defaulting with a loop is taking a lot longer hence the example above. Here is the correct version:


Graphics 800, 600

number = Input ("Press Enter to check speed of IF statements ")

begin# = MilliSecs()
Print "Start Time: " + begin#

For x = 1 To 5000000
If number = x Then Print "if check"

Next

If number = 0 Then endCheck# = MilliSecs()

result# = (endCheck# - begin#)

Print "End Time: " + endCheck#
Print "Result: " + result#
Print
Print

number = Input ("Press Enter to check speed of Select case statements ")

begin# = MilliSecs()
Print "Start Time: " + begin#

For x = 1 To 5000000
Select number
Case x
Print "select check"

Default

End Select
Next

endCheck# = MilliSecs()
result# = (endCheck# - begin#)

Print "End Time: " + endCheck#
Print "Result: " + result#

WaitKey


gellyware(Posted 2003) [#9]
Ok i think i understand now :) I should have thouroughly tested before posting. Strange enough, if you do the tests over and over, sometimes the select is a little faster, sometimes they are equal, but it seems if statements are .003-.010 milliseconds faster. I would conclude that they are both the same since it depends on what your cpu is up to at the time :) So choosing IF's vs. Select Case, is purely a personal choice :) Hope this has clarified.


_PJ_(Posted 2003) [#10]
Cheers, Bombone. I posted some code in the Archives precisely for checking the speed of functions in a similar way.

Interesting that 'If' is faster, despite all that has been said!

Thanks!


(tu) ENAY(Posted 2003) [#11]
> Interesting that 'If' is faster, despite all
> that has been said!

Well not really. Think of it like this

If blah then
else
; do blah
endif


In this example only two checks aximum are made. Only first if the first is true. Now look at this.

Select blah
Case 1
Case 2
Case 3
Case 4
Case 5
Case 6
Case 7
End select

If blah=7 then six seperate checks have be made before 7 is reached. SO if you had up to case 100. There are 99 checks before it.
To get around this you would simply order then in the most common order and also split it up.
If blah<25

Select blah
Case 0
Case 1
.
.
Case 24
End select

elseif blah<50

Select blah
Case 25
Case 26
.
.
Case 74
End select

elseif blah<75

;etc

else

;etc

endif


Then you only have a max of 25 checks.
However I wouldn't worry about it too much. It's not as essential these days to maximum searches in games.


big10p(Posted 2003) [#12]
I would say useage if select/case against If/else/elseif is purely down to personal preference. I would be surprised if there was any difference in execution speed between the two as internally all the same comparisions etc. have to be made. Any disparity in your testing prog will probably be down to OS interference.


Todd(Posted 2003) [#13]
Select/Case seems more useful, at least to me, because it can also do this:

Select number
Case 1,2,3
   ;Do Something
Case 4,5,6
   ;Do Somethine else
End Select


where if you were trying that with if statements then you would have to use a bunch of ORs.


Gauge(Posted 2003) [#14]
Good points if/endifs/else etc would slow it down alot. During the test your only comparing it to nothing or 0. the then is not needed either. I wonder what would happen if you ran ifs checking for types/arrays etc.