3

is there be any performance effect on "Lines of code - (C)" running inside nested ifs?

if (condition_1)
{
   /* Lines of code */ - (A)

   if (condition_2)
   {
      /* Lines of code */ - (B)

      if (condition_n)
      {
          /* Lines of code */ - (C)
      }
   }
}

Does that mean you can nest any number of if statements without effecting the execution time for the code enclosing at the end of last if statement?

user3244
  • 35
  • 1
  • 3
  • 7
    Micro optimization alert! – Etienne de Martel Jan 28 '11 at 20:37
  • "Performance effect" compared to what? Many times it's not very meaningful to talk about the performance of a piece of code without comparison to an alternative way of achieving the same logical result. How would you imagine writing this code differently while keeping the same results? – TheUndeadFish Jan 28 '11 at 21:36
  • 2
    @user3244: *micro-optimization* is a common term used to refer to the work spent in trying to gain performance by looking at the micro-detail, where (even if you could gain anything) the gain would not have any impact in the overall system. The best advice is: write correct easy to maintain code. Then if it runs too slow, *profile* and work on the algorithms. Only when the performance bottleneck is identified to be in a small piece of code that cannot be optimized (algorithm wise) think on *profiling again* and *then* work on the details. – David Rodríguez - dribeas Jan 28 '11 at 23:05
  • 1
    @user3244: Usually a high amount of nested blocks will make code harder to maintain, so, against what you are asking for, my advice would be to *break into functions* so that it is easier to understand. It could (or not) be marginally slower, but unless the code runs for a really long time in a tight loop that will not matter much. Every second you reduce from code maintenance in the code is worth over 2 billion CPU cycles. – David Rodríguez - dribeas Jan 28 '11 at 23:10
  • @user3244: from a maintenance point of view, I would consider not nesting, because it's difficult: `if (!cond_1) { return; }` allows to remove the block beneath it, for example. – Matthieu M. Jan 29 '11 at 10:02

3 Answers3

5

Remember C and C++ are translated to their assembly equivalents. In most cases, this is likely to be via some form of compare (e.g. cmp) and some form of jmp instruction.

As such, whatever code is generated from (C) will still be the same. The if nesting has no bearing on the output. If the resultant code is to generate add eax, 1 no matter how many ifs precede that, it will still be the same thing.

The only performance penalty will be in the number of if statements you use and whether or not the resultant assembly (jxx) is expensive on your system. However, I doubt that repeated nested use of if is likely to be a performance bottleneck in your application. Usually, it is time required to process data and or time required to get data.

  • I would emphasize that part of the transformation into assembly code usually result in optimizations so that the resulting assembly bears little ressemblance to the original code, if only that it products the expected result. – Matthieu M. Jan 29 '11 at 10:03
1

You won't affect the execution time of the indicated code itself, but if evaluating your conditions is complex, or affected by other factors, then it could potentially lengthen the total time of execution.

Harper Shelby
  • 16,475
  • 2
  • 44
  • 51
1

The code will run as fast as if it was outside. Just remember that evaluating an expression (in a if statement) is not "free" and will take a bit of time (more if the condition is more complex), so if your code is deeply nested it will take more time to reach it.

Jules Olléon
  • 6,733
  • 6
  • 37
  • 47