Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / The wonders of select case!

Author
Message
Tsproggy
12
Years of Service
User Offline
Joined: 2nd Jan 2012
Location: Reno, Nevada
Posted: 28th Jan 2012 19:56 Edited at: 29th Jan 2012 01:44
Hello all,

I'm here posting this to inform you about the benefits of using the select case commands when speed and efficiency are required. The reason I am doing this is I see quite a bit of people on here using if statements for nearly all of their checks in their respective games or programs.

I am fairly new to DarkBASIC Proffesional but I do have previous programming experience with multiple programming languages (C# is my favorite ). I simply just want to help you improve the ways your program/games can run!

Just like you, Computers read top to bottom. When you use an If statement your computer reads it line by line through a possibly insane amount of checks depending on how many you use.

For Example:



It will read those checks 1 by 1, which seems a waste if what you're comparing doesn't meet any of the conditions above. You just wasted some time you could spend calculating critical hits or something!

So now is where I get to introduce the wonders of the select case command! Unlike the if statement it doesn't go line by line. It checks your variable to see if it fits any it's conditions and if it matches any, it jumps to it and exits when told to. Which in turn makes it faster then the if statement.




With this it'll jump to what it needs. If you where using the above code in an if statement and pressing D(Down) it would have to check through 3 other keys before it got to you .

I hope you all understand what I've written!

I haven't failed 1,000 times! I've just found 1,000 ways something won't work.
Tsproggy
12
Years of Service
User Offline
Joined: 2nd Jan 2012
Location: Reno, Nevada
Posted: 29th Jan 2012 00:48
Wow.. I've had some bad luck with posting stuff on this forum for the last 2 days.. Yesterday half my message came out when posted.. and Today apparently the tags are case sensitive.

I vote there be an edit button with my post so I can fix these things.. and a preview button so I can make sure it works before I post it.

I haven't failed 1,000 times! I've just found 1,000 ways something won't work.
Millenium7
19
Years of Service
User Offline
Joined: 13th Dec 2004
Location:
Posted: 29th Jan 2012 04:27
are you sure it does this? after all the program needs to know 'where' to jump to. It doesn't automatically know to jump to line ~354 as opposed to ~332 when using the case command. Surely it's still checking all previous case statements top to bottom to find which one is correct before running the enclosed code

I think the reason why so many people (myself included) use the if statement is because its more readable and flexible. And in this day and age we are no longer coding for 386's with 1mb ram.

I'd like to see the difference in speed, but I have a sneaking suspicion that even over several thousand loops the difference in speed would be <1ms
Tsproggy
12
Years of Service
User Offline
Joined: 2nd Jan 2012
Location: Reno, Nevada
Posted: 29th Jan 2012 06:14
You would be wrong there sir! I have optimized a server emulator before where the coder used if statements for every check when a user right clicked an item in game. Most items in the game had a unique effect. These where literally around 1000+ items, I took out all the if statements and replaced the ones he had written in a neat Select Case(or switch) statement and I actually had quite an increase in latency (had 40+ milliseconds lag on my own local server -.-) If cut it down at least 12 ms, and you could notice a significant speed.

Also, when I used to do math programming challenges online, we would have to solve a problem or produce a result before a certain amount of time, So I would have to run a timer and stop it after I was done. I also noticed a significant difference with this.

You should try it some time, I know a select/switch statement is faster then an if statement. Also..

How is this:


easier to read then:


I haven't failed 1,000 times! I've just found 1,000 ways something won't work.
MrValentine
AGK Backer
13
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 29th Jan 2012 10:48
Nice...

Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 29th Jan 2012 16:22 Edited at: 29th Jan 2012 16:35
The select branch jumps to the literal that is identical to the result of the expression. The label of the case branch must BE literal ( EG: 1, 2, "3", "Cheese" or a constant: [#CONSTANT ONE = 1] ). Select 1 = 1, result is 1, so for each case, if case is 1, select case.

An if statement does not need to jump anywhere other than the line below or after the THEN keyword. If 1 = 1 Then DoThis

In other words; ask the question: If N is 10, is N lower than 3, lower than 6 and lower than 12. How could you write this easily in a select branch?



The above code will not work.

Although it is possible to do something like this:


It is recommended to use the following select statement over a series of if statements any day, it would run much faster.


If the previous results needed to be changed by the user, the if statement should be used:


Another handy use for select branches is to pick an array, or pointer to a variable, or a pointer to a function



BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Jan 2012 20:04
SELECT statements are useful, and become more efficient when used in a state machine scenario...

CASE cPLAYING
CASE cCOLLISION
CASE cSTART
CASE cEND

In this scenario we have definite states (rather than "less than 20"). By putting them in the order that they are most frequently used, those that are infrequently used are rarely executed. In the example above, if I am PLAYING 99% of the time, with the odd COLLISION, and only STARTing and ENDing once per game, I have prevented 75% of the code running 99% of the time. If you pad this out to include the dozens of statements required per state, it all makes sense!

Login to post a reply

Server time is: 2024-11-22 06:02:55
Your offset time is: 2024-11-22 06:02:55