Okay, I heard this person who had to be perfectly insane say something really weird... He said that if statements take up a lot of RAM... Now, being interested, I continued to listen... He said that it is actually more memory saving to write your own comparison function... Now, this just can't be! if statements is part of the core of C++, is it not? So, my question is, how would you writing an if statement function without an if statement? At some point you would need a Boolean return value of a comparison.... An if statement. So, is this possible and how?
Actually, I have another question... I have about 78 if statements that are running every frame in my game, and it is managing to slow it down... Every if statement has a break if it is true, and no more than one of them is true at a time... How would I optimize this? Thank you for you time and help!!
I don't know about exact RAM usage but I wouldn't worry too much about a few if statements, there are going to be many more things that will take up more memory then that in your program and I don't see many things you could write that would be more efficient.
The problem is more about the situations you use them in then the statements themselves. In a lot of cases there are better ways to write pieces of code without using 100 if statements in a line etc. Make sure you are using if-else if-else when you do use them together or switch statements when possible.
Showing exactly how to optimize code to use less if statements is hard without any samples to work off of.
The if statement literally translates into only a few assembly instructions that will run natively on the compiler. There is very little (read.. none) overhead in an if-statement.
Okay, I heard this person who had to be perfectly insane say something really weird... He said that if statements take up a lot of RAM...
Please present the complete argument you heard from the person
I have about 78 if statements that are running every frame in my game, and it is managing to slow it down... Every if statement has a breakif it is true, and no more than one of them is true at a time... How would I optimize this?
1. Can you predict which if statment will be true in the next frame?, if yes may be you can use function pointer, and keep changing them every frame
2. What does a typical if statement expresssion look like?, If is simple variable check then may be you can work out some kind of offset based jump mechanism
Okay, I'll try to attach the code... I was not having luck yesterday...
Okay... There we go. This is SFML code. The last huge if statement was simply to see if there is any difference in speed when putting it in one statement instead of 26... I saw no difference.
And unfortunately no... There is no way of predicting i the next key to be pressed, however, I can switch the shift and not shift... It is statistically more likely for a letter to not be caps..
If you have written such code as can be seen above with so many conditional checks, you need to ask yourself "there must be an easier solution to this?", as suggested by @cire and @Santosh. A basic rule of programming is that if the code looks nice 'n' tidy then the solution is probably the right one...