New submission from Maja <[email protected]>:
For example:
def f(x):
return 1 < x < 3
will be slower than:
def f(x):
return 1 < x and x < 3
The first function will generate following bytecode:
0 LOAD_CONST 1 (1)
2 LOAD_FAST 0 (x)
4 DUP_TOP
6 ROT_THREE
8 COMPARE_OP 0 (<)
10 JUMP_IF_FALSE_OR_POP 18
12 LOAD_CONST 2 (3)
14 COMPARE_OP 0 (<)
16 RETURN_VALUE
>> 18 ROT_TWO
20 POP_TOP
22 RETURN_VALUE
Performs unnecessary stack operations: duplicates x, rotates 3 items for no
reason, then re-rotates 2 items and pops a value. This is fine if the value in
the middle was more complex and needed to be duplicated rather than
recalculated, which would be definitely slower, but for simpler values like
names or constants, it's actually bad. The bytecode for the first function
should look the same as second one which is:
0 LOAD_CONST 1 (1)
2 LOAD_FAST 0 (x)
4 COMPARE_OP 0 (<)
6 JUMP_IF_TRUE_OR_POP 14
8 LOAD_FAST 0 (x)
10 LOAD_CONST 2 (3)
12 COMPARE_OP 0 (<)
>> 14 RETURN_VALUE
----------
components: Interpreter Core
messages: 404508
nosy: akuvfx
priority: normal
severity: normal
status: open
title: Using multiple comparison operators can cause performance issues
type: performance
versions: Python 3.11
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue45542>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com