Archived

This topic is now archived and is closed to further replies.

hi, i was wondering if you're program contains lots of "if..else", does it in anyway

This topic is 5747 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

''greatly'' reduces the speed of your game? i mean, what if you have a very long list of "if...else" statement in one particular state of your game? or this just affects a very small fraction of your game''s speed therefore it doesn''t matter? Liked to hear some advice from you guys so that if it does affect then I will reduce my "if...else statements..."(note: the DX common files have lots of this!)thanks people!

Share this post


Link to post
Share on other sites
If you put the most common condition on the first if, all the else's will be avoided in the code execution, most of the time.

Too much of anything will ultimately affect speed. But in the case of if-else's, the moment an if's condition is satisfied, that code within the block is executed and all the rest of the if-else block is avoided.


[edited by - Waverider on March 18, 2002 10:02:47 PM]

Share this post


Link to post
Share on other sites
Its probably not going to kill you, but if you have a ton of if-else statements you proably could orginize your code a bit better. Or rather, what you are trying to do could probably be done a better way. Though this isnt always true.

I wouldnt worry about it. Go with what you got and keep learning. 6 months from now you''ll look back at this and laugh.

Ratman

Share this post


Link to post
Share on other sites
When your new, it seems every program becomes a large mess of if/else''s. With practice you''ll learn how to better encapsulate everything into algorithms, classes, and Finite State Machines (what I have been using a lot lately w00t!). If statements are very cheap, so don''t worry about the speed loss.

When I develop I try to follow this philosophy. If its graphics, make it fast and mean, else make it lean and clean. Anything graphical eats up lots of cycles. If you profile your project you''ll be surprised how little of your time is spent in big chunks of logic code compared to just 1 blit call.

- Kevin "BaShildy" King
Game Programmer: DigiPen
www.mpogd.com

Share this post


Link to post
Share on other sites
hey guys thanks for all your thoughts!

actually same with the switch case statements, i just want to know if I really must ''do something'' when my codes has too many logic checkings,

Share this post


Link to post
Share on other sites
You could use the ternary instead of a if/else statement. I did read some where it was faster. Im not sure about using nested ternary operations as a good idea though.




int y = 7;
int x;

if(y > 5)
x = 1;
else
x = 0;

x = ((y > 5) ? 1 : 0);


-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."

Share this post


Link to post
Share on other sites
You could use the ternary instead of a if/else statement. I did read some where it was faster. Im not sure about using nested ternary operations as a good idea though.




int y = 7;
int x;

if(y > 5)
x = 1;
else
x = 0;

or

x = ((y > 5) ? 1 : 0);


-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."

Share this post


Link to post
Share on other sites
The ternary operator is not faster than if-then statements.

Under gcc with optimisations off:


b = (a == 56) ? 1 : 2;

cmpl $56,-4(%ebp)
jne L3
movl $1,%eax
jmp L4
.p2align 4,,7
L3:
movl $2,%eax
L4:
movl %eax,-8(%ebp)



if(a == 56)
b = 1;
else
b = 2;

cmpl $56,-4(%ebp)
jne L5
movl $1,-8(%ebp)
jmp L6
.p2align 4,,7
L5:
movl $2,-8(%ebp)
L6:


So, with optimisations off, the ternary operator is -slower- than an if-then statement!

With optimisations on, they compile to the same code:

movl $2,%eax
cmpl $56,-4(%ebp)
jne L3
movl $1,%eax
L3:

Share this post


Link to post
Share on other sites
Good point Beer Hunter. I will recheck the book I saw it in and post what it states. Also, did you try it under MSVC?


-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."

Share this post


Link to post
Share on other sites
A _simple_ ''if'' statement uses on average 1.5 assembly language instructions. Now, your computer can perform millions of assembly language instructions per second (depending on the processor maybe billions) An ''if'' statement is
not going to affect performance a great deal at all.

''Your theory of a donut shaped universe is intriguing Homer'' - Stephen Hawking

Share this post


Link to post
Share on other sites
The problem with ''if'' statements, is that they do not only produce 2 assembly opcodes. They mess with branch prediction, and this can kill you, depending on what you do.

I would avoid *huge* if/else trees. Besides of the performance hit, it''s also very ugly and points to a design flaw in your code. A good way to replace if/else trees that are based on comparing a simple value are function pointer tables.

So, instead of writing

if( v==1 ) DrawSphere(); else
if( v==2 ) DrawCylinder(); else
if( v==3 ) DrawTeapot(); else
if( v==4 ) DrawStuff(); else
...

you do

(*FunctionPointerTable[v])();

So much easier and faster.

Share this post


Link to post
Share on other sites
function ptrs are not always faster, but are much cleaner. plus switch statments get broken into a jmp table anyway, so the speed difference is not too great if any at all, especially if your not doing much in the case (ie just making an assignment). functions require pushing stuff on the stack and other crap that could reduce speed. stay away from fuction ptrs unless you are actaually going to have decent functions (ie like drawing stuff).

Share this post


Link to post
Share on other sites
quote:
Original post by mickey
whoa what was that..

Its called Assembly language. Raw code, if you will All the instructions (mostly 3 leter words) you see are directly executed one-by-one, simutaneusly incresing the SI register (i think its called SI, not shure). The CPU does the instruction always pointed to the SI register (points to the code execution memory offset).

[ my engine ][ my game ][ my email ]
SPAM

Share this post


Link to post
Share on other sites
a person:

I wouldn''t use function pointers for easy case statements, if it''s only an assignment, a lookup table could be used after all. But for more complex stuff, they are very useful. Esp. when replacing huge if/else or switch trees, eg. for commandline interpreters (shells), and such things.

quote:

Beer: only in 32-bit systems, I believe. Otherwise it has a different name. Either way, it''s the instruction pointer.


In 32 bits: EIP
In 16bits: IP, (implicit) segment prefix is cs

Just as AX, EAX, SP, ESP, etc.

Share this post


Link to post
Share on other sites
Don''t get the guy using function pointers if they''re not needed - most compilers will create a jump table for large switch constructs.

Just use switch.

Share this post


Link to post
Share on other sites
quote:
Original post by ZealousElixir
Beer: only in 32-bit systems, I believe. Otherwise it has a different name. Either way, it''s the instruction pointer.


We were discussing 32-bit i386 code...

Share this post


Link to post
Share on other sites