-4

Which one takes more time to compile in python? This one?

if age > 30:
    if height > 5:
        print('perfect')

or this one?

if age > 30 and height > 5:
    print('perfect')
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 1
    Welcome to Stack Overflow. This doesn't make any sense as asked. Time complexity has nothing to do with "compiling", and it also is completely irrelevant here. Time *complexity* does not mean how long it takes to run the code; it means *how that time changes when the input gets bigger*. But in this code, the input is fixed, and can't get bigger anyway. Even then, it is worrying about something utterly meaningless. Either way of doing the `if`s is much, much faster than the `print` itself. – Karl Knechtel Sep 01 '22 at 10:25
  • 1
    Between the two the time difference is negligible as stated in some answers. However, if you have statistical information on how probable is each conditional to evaluate true, then putting the conditional with higher probability to evaluate `False` first will make it faster, since the `and` condition will short-circuit more often. For `or` conditions, putting the condition that evaluates `True` more often first will make it faster. But unless the conditionals are clearly biased you should not spend much time in this premature optimizations –  Sep 01 '22 at 10:32

5 Answers5

2
Python 3.8.10 (default, Jun 22 2022, 20:18:18) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def x():
...     if age > 30 and height > 5:
...         print('perfect')
... 
>>> def y():
...     if age > 30:
...         if height > 5:
...             print('perfect')
... 
>>> import dis
>>> dis.dis(x)
  2           0 LOAD_GLOBAL              0 (age)
              2 LOAD_CONST               1 (30)
              4 COMPARE_OP               4 (>)
              6 POP_JUMP_IF_FALSE       24
              8 LOAD_GLOBAL              1 (height)
             10 LOAD_CONST               2 (5)
             12 COMPARE_OP               4 (>)
             14 POP_JUMP_IF_FALSE       24

  3          16 LOAD_GLOBAL              2 (print)
             18 LOAD_CONST               3 ('perfect')
             20 CALL_FUNCTION            1
             22 POP_TOP
        >>   24 LOAD_CONST               0 (None)
             26 RETURN_VALUE
>>> dis.dis(y)
  2           0 LOAD_GLOBAL              0 (age)
              2 LOAD_CONST               1 (30)
              4 COMPARE_OP               4 (>)
              6 POP_JUMP_IF_FALSE       24

  3           8 LOAD_GLOBAL              1 (height)
             10 LOAD_CONST               2 (5)
             12 COMPARE_OP               4 (>)
             14 POP_JUMP_IF_FALSE       24

  4          16 LOAD_GLOBAL              2 (print)
             18 LOAD_CONST               3 ('perfect')
             20 CALL_FUNCTION            1
             22 POP_TOP
        >>   24 LOAD_CONST               0 (None)
             26 RETURN_VALUE
>>> 

In my test, they produced identical compiled bytecode.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
1

Boolean conditions are evaluated using short-circuit logic. Any performance difference between the two would be negligible, if any.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Both the conditions are equivalent even in the case of time complexity. I'm attaching another post and you may view that both cases are equivalent since at compile the instruction sets are same. nested if vs. and condition

Muhammad Ahmed
  • 318
  • 2
  • 8
0

This is what basically happens when you compare stuff with and:

def _and(*args):
    for arg in args:
        if not arg: return arg
    return arg

As you can see, it does the same as the nested ifs. Therefore, there wouldn't be much difference between the two.

The Thonnu
  • 3,578
  • 2
  • 8
  • 30
0

I'm not an expert for python in any way, but based on my knowledge of compilers for C and C++, here is my answer.

When you write a logical condition, the compiler will try to load the code it thinks is the most likely to come up next, in order to run faster. so if you write

if (age_of_bob > 200) {
     foo()
} else {
     bar()
}

since the guy named bob has very little chance of being over 200 years old, the compiler will try to preload the bar() code instead of foo(). (the example is trash but you get it) Of course this doesn't always work, but compilers are smart and very often, they load the correct code in advance. (read about instruction pipelining and branchless programming)

so in your example, which is python and not C, the interpreter would have to make this kind of guess twice in the first example, and once in the second. Of course this would matter more if you had else clauses.

Now, this is only a guess as I'm not quite sure that the python interpreter does things like the gcc compiler, but if there is a difference, it could come from here. The only way to make sure, is to do a benchmark. Beware of testing only this section of the code and not the whole code in case you have other variables that may change the results. Run it 100000 times and check if there really is a difference.