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')
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')
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.
Boolean conditions are evaluated using short-circuit logic. Any performance difference between the two would be negligible, if any.
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
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.
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.