views:

92

answers:

5

Hi

Consider these to options

if(successful)
{
    if(condition)
    {
       //do something
    }
    if(condition)
    {
       //do something
    }
    ...
}

or

if(successful)&&(condition)
{
   //do something
}
if(successful)&&(condition)
{
   //do something
}
...

Imagine there 100 if statements.

Is there any difference in efficiency?

Thanks in advance.

+8  A: 

That all depends how costly it is to evaluate the successful expression.

You should also note that the two versions are not semantically equivalent as the evaluation of the if-expression might have side-effects1.

If you are actually facing performance issues then measure, don't guess. Measuring will be the only way to see what the performance really is.

1To explain a question from the comments, here is a simple example where you would get different behavior:

The method CreateProcess has the side-effect of starting a new process and indicates the successful creation by returning true:

bool CreateProcess(string filename, out handle) { ... } 

if (CreateProcess("program.exe", out handle))
{   
    if (someCondition)
    {
         handle.SomeMethod(...);
    }
    if (someOtherCondition)
    {
         handle.SomeOtherMethod(...);
    }
}

This is quite different from the following:

if (CreateProcess("program.exe", out handle) && someCondition)
{
    handle.SomeMethod(...);
}
if (CreateProcess("program.exe", out handle) && someOtherCondition)
{
    handle.SomeOtherMethod(...);
}
0xA3
Amen, brother: this smells like an extreme case of irrelevant micro-optimization.
Piskvor
I'm not facing any problems. Just thought id ask before i started doing it. What do you mean when you say there not equivalent?
Ash Burlaczenko
Some compilers can sometimes factor the second form into the first when the shared expression has no side effects, so the runtime cost of evaluation may not matter. Nevertheless, the essential point is correct: measure, don't guess.
Ian
@Ash Burlaczenko: See the update for an example where the two versions behave differently.
0xA3
Ok point taken. That wouldn't matter if the first condition didn't call anything though, say it was just bool condition1 = true;
Ash Burlaczenko
+1  A: 

Both are O(1). Anything else is depends on the language/compiler/optimizer you use.

nikie
Careful: Big-O-notation does not make a statement about performance! It tells you how an algorithm scales depending on the input size. An O(n^2) algorithm can be faster than an O(1) algorithm depending on how large your input is.
0xA3
@0xA3: I think that was precisely the point nikie was making.
Gian
+9  A: 

There are two correct answers for this. Everything else is nonsense.

  1. Stop worrying about micro-optimizations like this unless you have proven the need for them. This can only be done by measuring and confirming that the code you're looking at is a bottleneck. (Hint: your intuitions in matters like this are almost, but not quite, always wrong.)
  2. If you have successfully proven that your code is a bottleneck, try both ways and measure the results. Nobody here is going to be able to answer this question for you unless they happen to have identical hardware running on an identical operating system and are compiling with an identical compiler.

Make your code correct first. Then measure it for performance. Then optimize if needed. Everything else is nonsense.

JUST MY correct OPINION
+1 for not stating that the correct answer is 42
belisarius
A: 

Let me start out with stating that I completely agree with JMcO

However I find it can be interesting to think about the differences. (Might be because I working on a compiler, where the statement about optimizing (output) has to be done upfront and not based on measurements but assumptions on/knowledge of the general use of the compiler)

There's no one answer to your question, there's simply to many aspects that might affect the performance.

  • is success a value or a method call
  • Does the compiler create short circuiting for && operations (bailing out if the lhs is false) or does it always evaluate right and left hand side of the &&
  • What kind of branch prediction is the processor using?
  • How's the compiler treating compounded conditions vs nested? The compiler might produce the same binaries for the two examples you're providing though it most like won't (when the compiler can verify that the outer condition has no side effects)
  • What kinds of optimizations are the compiler performing? Could some of them radically change the binaries (e.g. skip the condition if it can be found to be constant or at least known before hand?)

the list could be elongated but my point is eventhough it can be interesting to think about what could affect the performance with out knowing a lot about both build and execution environment it's basically impossible to predict performance

Rune FS