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 AppGameKit Corner / Is one faster than the other? Select or several IF?

Author
Message
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 7th Mar 2017 09:21
Hello For optimization, would Select be faster than several IF? If I have 4 IF, they all get checked, but what if I had a Select with 4 cases, would that be faster? I am hoping that Select will "jump" to the right case, and not check each case until it finds a match. Can anyone tell me how select works in the "machine room"?
13/0
hoyoyo80
7
Years of Service
User Offline
Joined: 11th May 2016
Location:
Posted: 7th Mar 2017 11:40
Nice topic..would love to know too.
IronManhood
9
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 7th Mar 2017 14:53 Edited at: 7th Mar 2017 15:00
Select would be faster if you were dealing with hundreds or thousands of cases because the program isn't forced to evaluate all the if statements until one passes. However, if you are only dealing with a small number of cases then the difference is negligible. I don't know how select is implemented in agk. It would be lovely to know.
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 7th Mar 2017 14:58
Okay, thank you for the good answer
13/0
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 7th Mar 2017 15:12
I have been reading some more about case VS if, it seems that it is language and compiler specific how it is implemented. One compiler will create a jumptable when there are more than five items. So my next question is: What does the AppGameKit 2 Tier 1 compiler do with cases? Does it create a jumptable when there more than X items?
13/0
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 7th Mar 2017 16:16
Nice topic. Although my expectations from an interpreter like AppGameKit tier 1 are not great
BatVink
Moderator
20
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Mar 2017 16:35
Generally a Select statement is quicker. But it is important to order the cases into most frequently used. For example, cases for the game state are often listed like so:

Initialise
StartGame
PlayGame
EndGame
Deinitialise

But they should be

PlayGame
StartGame
EndGame
Initialise
Deinitialise

because PlayGame is selected 99% of the cycle.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Mar 2017 18:16
I would agree the difference is negligible. The real speed difference comes from what Batvink suggested, the order you put the statements. Ideally, you would try to have the most common results first and the least likely at the bottom. Because the sooner a condition is met, the sooner it can stop checking the rest. This of course assumes you're using IF ELSE and not just a series of IF statements, in which case it'd make no difference.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Crazy Programmer
AGK Developer
19
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 7th Mar 2017 18:55 Edited at: 7th Mar 2017 18:58
Take a look at Daniel TGC's thread. He Has created a speed test to figure out the question you are asking. Like Batvink said though, its all bout the structure.

https://forum.thegamecreators.com/thread/215347
Download My Games for Android. Made with AGK.
Jellyfish Dive- https://play.google.com/store/apps/details?id=com.CrazyProgrammerProductions.my_JellyFishSwim
Brick Destroyer - https://play.google.com/store/apps/details?id=com.CrazyProgrammerProductions.my_BrickDestroyer
Ping Bong - https://play.google.com/store/apps/details?id=com.CrazyProgrammerProductions.my_Pong_Ping_Bong
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 7th Mar 2017 21:55
Good tip Mr Vink.

Login to post a reply

Server time is: 2024-03-29 11:12:48
Your offset time is: 2024-03-29 11:12:48