Advertisement

Optimizing IFs

Started by February 18, 2000 10:03 AM
13 comments, last by Yanroy 24 years, 7 months ago
Is there a better way to do checks than using if? It is really slow, and for most of the places where I am using it, a switch won''t work either, because they don''t take variables in the case statement. --------------------
"Very funny Scottie, now beam down my clothes." www.trak.to/rdp

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

if(you have to use an if)
{
You''re shit out of luck.
}

You could however think of a creative way to not use an if, or reduce the number of if''s.

Good luck!
Advertisement
asm cmp, i

Edited by - Gromit on 2/18/00 10:54:00 AM
William Reiach - Human Extrodinaire

Marlene and Me


It''s not the if that''s slow, per se... but the branch instructions it generates. Most "creative" alternatives still end up doing that (switch, for example) so unless you can really rethink the way that you''re doing things, you''re probably stuck with it. If you have any simple examples from your code, someone might have an idea. (I''d be happy to look at it.)

-Brian
Hello

As you are talking about If''s, I want to ask something I wanted to know for some time :

Lets say I have a loop with an int (call it x) that has the value 5 half of the cycles, and the other half it is an random value. If I put an :

if (x != 5)
x = 5;

Will I just slow it down, than a simple :
x = 5; ?

If you are cursing if so much, it seems that it will slow down... But I just want to be sure

Thanks a lot,
-RoTTer
RoTTer - Yes, the if will slow things down in that case. You have to think about the machine code being generated in each case:

if (x != 5)
x = 5;

Produces something like:
SUBTRACT X, 5
CONDITIONAL BRANCH (Last result == 0?)
SET X, 5
[BRANCH TARGET]

---

Versus something like:

x = 5;

which just produces

SET X, 5;

-Brian
Advertisement
in what dimensions are you talking when you mean slow down? nanoseconds? even shorter?
isn''t it so that nobody will ever see a difference wether you are using 500 ifs to make your code safer/better readable or you don''t?
well, i don''t know how slow an if is, but it can''t be THAT slow that you are not going to use it
If you''re having doubts about the performance of any code just put a comment on it, e.g. "// Need optimization!", so that you can find it later when it''s time for optimization.
Until it''s time, it is better to write readable code.
if''s aren''t really slow at all. There is really only 1 place where they are on modern computers. Basically there are 3 different scenarios.

1) An if that only happens once, or a small # of times per main loop pass. These cosume so little time they are negligble.

2) In a small loop, iterated many times, but where the same branch is taken most of the time. This is also fairly fast, because the processor keeps track of the previous branch, and next time around will assume you''ll go the same way. So if you do: no delay at all.

3) Small loop, but the branch taken is unpredictable. This one can be very costly, because the pipeline, in theory, can be broken on every pass. Try to think you''re way around these. (A good example of this is drawing a transparant sprite by checking each pixel for 0 to determine whether to draw it or not)

Usually, if''s are very fast, and can''t, nor should they be, avoided.

Rock
if statements, just like any other CPU command, create a noticeable slowdown only if they''re executed very frequently. So, for example, if your pixel-plotting function checks the surface bit depth each time it draws a pixel, that''s very slow. It would be better to create a DrawWorld16 and DrawWorld24, for example; then, each function will call its down DrawTile16/24 and DrawPixel16/24 without needing any if statements. This means a somewhat large file size, since there''s more code, but it will run much faster.

It''s unlikely anyone will have this extreme situation, but it''s a good example.

~CGameProgrammer( );

~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement