Will too much IF STATEMENT decrease performance

This is a discussion on Will too much IF STATEMENT decrease performance within the C++ Programming forums, part of the General Programming Boards category; This is related with the project I'm working on at work. Performance and reliability is a big issue for my ...

  1. #1
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31

    Will too much IF STATEMENT decrease performance

    This is related with the project I'm working on at work.

    Performance and reliability is a big issue for my project.
    I need to do a lot of condition check in my program in-order to produce valid output and ensure all undesireable condition needs to get handled.

    Therefore, there is a lot of IF condition in many of my function, and a lot of them is nested IF statement.

    I try to look for some reference online and book, but it seems like there little to none information about it.

    What do you guys think? Do you think IF STATEMENT will decrease program run-time performance?

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    If statements can be a big performance hit depending on the system. Now with the branch prediction routines if you have a lot of jumps it can cause cache misses and then cache has to be cleared and reloaded with new information. You can look up Branch prediction to get more details but other then that from what I have read and understand it depends on the structure of your jumps. are you putting the most common case first so it skips all the other cases. Try to factor out as many jumps as possible.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    They could give you a performance hit, but what alternative could you use? The ifs cannot very well be ripped out. All you would be able to do what manofsteel972 suggested. Unless you're on some sort of embedded system, it's not something you should worry about too much.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I'm not a compiler expert, but an if-statement shouldn't generate that much machine code. It requires less overhead and processing than a function-call.

    In general, a few "extra" statements should not be a problem, unless you are performing an iteration, or if you're in a loop. For example, if you are processing a bitmap, you would want to minimize the code gets executed once per pixel.

    The branch prediction stuff only affects multi-threaded code... where one thread is executing code ahead of the branch. With regular 'ol single-threaded code, everything happens one step at a time in sequence, so there is no need to predict... Program flow gets to the branch, and takes one branch or the other.

    [B][EDIT] - [B] Actually, manofsteel might be right. Dependong on how "far" the address pointer to "jump", the code for one branch or the other might not be in cache.
    Last edited by DougDbug; 05-18-2005 at 05:05 PM.

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    It's not just multi-threaded code, it's any code that is being run on a processor that runs out-of-order execution. The Pentium for instance does a great job at branch prediction and will guess what the outcome of an comparison will be, and then proccess a few lines of code based on that prediction. But if that prediction was wrong, the processor has to stop itself, refetch, and decode another set of operations.

    Yeah, not too much of a big deal, but it's something that you should keep in mind when trying to optimize.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Do a search on optimization. Basically if the project is done don't worry about minimal optimzations. You should be worried about the algorithms used and whatnot. If states are fairly quick in of itself. Granted if you are comparing a user defined that takes a long time to return a boolean result then yeah a lot of ifs will be a performance hit.

  7. #7
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31
    Thanks for everyone for replying.

    I'll look into it tomorrow when I go back to work and maybe factor out some of the IF statement. Put the most common case first, and the less common case after.

  8. #8
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    http://c2.com/cgi/wiki?PerformanceOfConditionalLogic Here is a link I found that might be more informative.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I need to do a lot of condition check in my program in-order to produce valid output and ensure all undesireable condition needs to get handled.
    I agree with DougDbug. Unless you've run your code and see a performance problem, you're better off leaving in the extra if statement checks to make your code more reliable. If when you run your code you see a performance problem, then most experts recommend using a profiler to see what section of code is taking the most time. Then you can optimise that section.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Use bit Signature

    You can use bit Signature to convert if statements... still it depends on your problem, however you can use Bit signature as alternative each having an action if that success .. like if you have 10 if (and else) you can have 10 bits to represent your problem and say a particular signature execute that code .. Bit calculations are faster and it also provides much cleaner code ...

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Or there is reading the forum rules about bumping long dead threads.
    Closed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Performance and footprint of virtual function
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 06:34 PM
  2. File map performance
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2008, 03:18 AM
  3. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  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, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21