views:

144

answers:

5

Is there an efficiency difference between using and in an if statement and using multiple if statements? In other words, is something like

if expr1 == expr2 and expr3==expr4:
  dostuff()

different from an efficiency standpoint then:

if expr1 == expr2:
  if expr3 == expr4:
    dostuff()

My very basic testing does not reveal a difference, but does someone with more knowledge (or at least more thorough testing) have a definitive answer?

+2  A: 

In either case, if expr1 == expr2 evaluates to false, the 2nd will not be evaluated.

Matthew J Morrison
+11  A: 

This isn't enough of a performance difference, if any, to affect your decision. IMO, the decision here should be made purely from a readability perspective. The first is generally more standard, I think, but there are situations when the second might be clearer. Choose the method that best gets your intent across.

froadie
s/IMO//. It's absolutely insane to choose one of these over the other for performance reasons, in pretty much **any** language. There is no way to quantify which will be faster, the implementation can do what it wants.
Longpoke
Thanks. Generally, I agree and I focus on readability over efficiency. But in this particular case, the if is in a loop being executed a sizable number of times and I have a requirement to reduce the runtime so, at least this time it might matter.
TimothyAWiseman
+2  A: 

The first one (one if with and) is faster :-)

I tried it out using timeit. These are the results:

Variant 1: 9.82836714316
Variant 2: 9.83886494559
Variant 1 (True): 9.66493159804
Variant 2 (True): 10.0392633241

For the last two, the first comparision is True, so the second one is skipped. Interesting results.


import timeit


print "Variant 1: %s" % timeit.timeit("""
for i in xrange(1000):
    if i == 2*i and i == 3*i:
        pass
        """,
        number = 1000)

print "Variant 2: %s" % timeit.timeit("""
for i in xrange(1000):
    if i == 2*i:
        if i == 3*i:
            pass
        """,
        number = 1000)

print "Variant 1 (True): %s" % timeit.timeit("""
for i in xrange(1000):
    if i == i and i == 3*i:
        pass
        """,
        number = 1000)

print "Variant 2 (True): %s" % timeit.timeit("""
for i in xrange(1000):
    if i == i:
        if i == 3*i:
            pass
        """,
        number = 1000)
leoluk
+3  A: 

Any differences in speed between using and and nested ifs will be minimal. You are barking up the wrong tree. Consider this tree:

if oftenTrueCondition and rarelyTrueCondition:

compared with

if rarelyTrueCondition and oftenTrueCondition:

So, unless the first condition must be evaluated first (it is a guard to stop the next expression from crashing or doing something silly/expensive), consider swapping the order of evaluation.

John Machin
A very useful way to think about this that I hadn't considered, and actually will come in handy in numerical work. Thanks!
Spencer Nelson
+2  A: 

When in doubt, you can check what does python compile your statements in, using dis module:

>>> import dis
>>> def test1():
...     if expr1 == expr2 and expr3==expr4:
...        dostuff()
... 
>>> def test2():
...     if expr1 == expr2:
...        if expr3 == expr4:
...           dostuff()
... 
>>> dis.dis(test1)
  2           0 LOAD_GLOBAL              0 (expr1)
              3 LOAD_GLOBAL              1 (expr2)
              6 COMPARE_OP               2 (==)
              9 JUMP_IF_FALSE           24 (to 36)
             12 POP_TOP             
             13 LOAD_GLOBAL              2 (expr3)
             16 LOAD_GLOBAL              3 (expr4)
             19 COMPARE_OP               2 (==)
             22 JUMP_IF_FALSE           11 (to 36)
             25 POP_TOP             

  3          26 LOAD_GLOBAL              4 (dostuff)
             29 CALL_FUNCTION            0
             32 POP_TOP             
             33 JUMP_FORWARD             1 (to 37)
        >>   36 POP_TOP             
        >>   37 LOAD_CONST               0 (None)
             40 RETURN_VALUE        
>>> dis.dis(test2)
  2           0 LOAD_GLOBAL              0 (expr1)
              3 LOAD_GLOBAL              1 (expr2)
              6 COMPARE_OP               2 (==)
              9 JUMP_IF_FALSE           28 (to 40)
             12 POP_TOP             

  3          13 LOAD_GLOBAL              2 (expr3)
             16 LOAD_GLOBAL              3 (expr4)
             19 COMPARE_OP               2 (==)
             22 JUMP_IF_FALSE           11 (to 36)
             25 POP_TOP             

  4          26 LOAD_GLOBAL              4 (dostuff)
             29 CALL_FUNCTION            0
             32 POP_TOP             
             33 JUMP_ABSOLUTE           41
        >>   36 POP_TOP             
             37 JUMP_FORWARD             1 (to 41)
        >>   40 POP_TOP             
        >>   41 LOAD_CONST               0 (None)
             44 RETURN_VALUE        

So as you can see, at python bytecode level, both statements are same - even while you use single if at first statement, it will do JUMP_IF_FALSE after first comparison.

Daniel Kluev