Thread: If statement slowdowns?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    118

    If statement slowdowns?

    I was just wondering if many"IF" statements can slow down code considerably, and if so, how many would it take to notice a slight slow down on a moderate computer - say a P3 400 MHZ system? The reason I ask is because in my tile engine I have used a lot of if statements to read the 2D tile array. I don't notice any slow downs as of yet, probably because my computer is quite fast. I am using DirectX 7 if anyone wants to know, or if it is important in any way. I probably could use something other than "if" statements, but im still in the early newbie stages. Is there anything else I can use that someone could recommend that would be faster - it would be greatly appreciated? Thanks in advance!
    Last edited by Tommaso; 04-12-2003 at 11:25 PM.
    "The distinction between past, present and future is only an illussion, even if a stunning one."
    -Albert Einstein

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    the if statement itself won't slow you down.. it's technically what you're testing that will slow you down.. minimize the needed tests and you should be fine. If statements are never the bottleneck of code, and at your stage worry more about getting the game working than optimizing.

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    I agree with cozman but the fewer decisions you have to make in code the faster things will run.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you're using several if's, be smart how you place them. Place the ones taking less time first and the slower ones last to reduce the frequency the slower ones is run.

    If you need to test two conditions:
    Code:
    if(FastIfStatement)
    {
       if(SlowIfStatement)
       {
          ...
       }
    }
    Code:
    if(ThisOccursOften)
    {
       ...
    }
    else if(ThisOccursSometimes)
    {
       ...
    }
    else if(ThisOccursRarely)
    {
       ...
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    17
    Originally posted by Magos
    Code:
    if(FastIfStatement)
    {
       if(SlowIfStatement)
       {
          ...
       }
    }
    or even:
    Code:
    if(FastIfStatement&&SlowIfStatement)
    {
        ....
    }
    which is just as fast and like the one by magos, the second one isnt even tested if the 1st one is false
    Drink Yer Wilk

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    118
    Your suggestions are great. That should help me out. I never would have thought of saving time that way. It's so simple, yet should save a few clock cycles. Thanks!

    Another question I am after is that in my tile engine, I use IF statements for every tile that can be blitted in my 2D map array. There is 40 tiles in all. Is this too many IF statements? I know I can use CASE but that's pretty much the same, just looks better - am I right? And, is there something else I should be using that seems more professional? It just seems not right to see all those IF statements in one chunk of code. Thanks in advance.
    "The distinction between past, present and future is only an illussion, even if a stunning one."
    -Albert Einstein

  7. #7
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by Silvercord
    I agree with cozman but the fewer decisions you have to make in code the faster things will run.
    Oh yes, how about this one:
    Code:
    while ( i = 0;;i++)
    {
       if ( i == 1 ) break;
    }
    This if will slow down the code, isn't it?

    Originally posted by Wilk
    Code:
    if(FastIfStatement&&SlowIfStatement)
    {
    ...
    }
    which is just as fast and like the one by magos, the second one isnt even tested if the 1st one is false
    This is true for Magos his version as well.
    Besides, there is a logical AND in addition, so it takes a few cycles more to the CPU to evaluate the expression.

    On the other hand, it takes less space in the editor, maybe it seems to be even more readable for some of you (I don't like embedded if's either) - it becomes a performance issue only if it is executed many times.

    Tommaso, to answer your last question, I usually prefer switch / case instead of if when there are more then 2 "if"-s.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    if your worried about too many if statements and your evaulating integers (even if your not, you can just find a way to use integers, or modify this to work) just create an array of pointers to functions, where Array[0] is the function to call when the testing variable is 0

    then you just call the Array[Integer] function, removing all If's in that area, and replacing it with a * then an +.... very fast

    but this only works if your dealing with consecutive integers, otherwise the array becomes realy large.

    but still, i use it a fair amount.... grate for calling attack moves in RPG games based upon what weapon you have
    ie
    bow = 1
    sword = 2

    call Attack[Sword]
    My current project is an RPG game similar to Diablo, Arcanum and Baulders Gate. Still very much in the planning stage. Anyone has info on Memory Management for games, please PM me, thx

  9. #9
    Switches can actually be faster. Depending on your compiler they may be translated into a jump table, which will be FAST.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM