Last active
October 26, 2023 21:41
-
-
Save tsibley/e5230c9f47793c0682eb13343b8c26ce to your computer and use it in GitHub Desktop.
Python optimization levels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""\ | |
DOCSTRING\ | |
""" | |
print(__doc__) | |
if __debug__: | |
print("DEBUGGING") | |
if not __debug__: | |
print("OPTIMIZING") | |
assert False, "ASSERT" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python3.11 -m dis example.py | |
0 0 RESUME 0 | |
1 2 LOAD_CONST 0 ('DOCSTRING') | |
4 STORE_NAME 0 (__doc__) | |
5 6 PUSH_NULL | |
8 LOAD_NAME 1 (print) | |
10 LOAD_NAME 0 (__doc__) | |
12 PRECALL 1 | |
16 CALL 1 | |
26 POP_TOP | |
7 28 NOP | |
8 30 PUSH_NULL | |
32 LOAD_NAME 1 (print) | |
34 LOAD_CONST 2 ('DEBUGGING') | |
36 PRECALL 1 | |
40 CALL 1 | |
50 POP_TOP | |
10 52 NOP | |
13 54 LOAD_ASSERTION_ERROR | |
56 LOAD_CONST 5 ('ASSERT') | |
58 PRECALL 0 | |
62 CALL 0 | |
72 RAISE_VARARGS 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python3.11 -O -m dis example.py | |
0 0 RESUME 0 | |
1 2 LOAD_CONST 0 ('DOCSTRING') | |
4 STORE_NAME 0 (__doc__) | |
5 6 PUSH_NULL | |
8 LOAD_NAME 1 (print) | |
10 LOAD_NAME 0 (__doc__) | |
12 PRECALL 1 | |
16 CALL 1 | |
26 POP_TOP | |
7 28 NOP | |
10 30 NOP | |
11 32 PUSH_NULL | |
34 LOAD_NAME 1 (print) | |
36 LOAD_CONST 4 ('OPTIMIZING') | |
38 PRECALL 1 | |
42 CALL 1 | |
52 POP_TOP | |
54 LOAD_CONST 5 (None) | |
56 RETURN_VALUE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python3.11 -OO -m dis example.py | |
0 0 RESUME 0 | |
1 2 NOP | |
5 4 PUSH_NULL | |
6 LOAD_NAME 0 (print) | |
8 LOAD_NAME 1 (__doc__) | |
10 PRECALL 1 | |
14 CALL 1 | |
24 POP_TOP | |
7 26 NOP | |
10 28 NOP | |
11 30 PUSH_NULL | |
32 LOAD_NAME 0 (print) | |
34 LOAD_CONST 3 ('OPTIMIZING') | |
36 PRECALL 1 | |
40 CALL 1 | |
50 POP_TOP | |
52 LOAD_CONST 4 (None) | |
54 RETURN_VALUE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pr --omit-pagination --merge --page-width=180 opt-[012] | |
# python3.11 -m dis example.py # python3.11 -O -m dis example.py # python3.11 -OO -m dis example.py | |
0 0 RESUME 0 0 0 RESUME 0 0 0 RESUME 0 | |
1 2 LOAD_CONST 0 ('DOCSTRING') 1 2 LOAD_CONST 0 ('DOCSTRING') 1 2 NOP | |
4 STORE_NAME 0 (__doc__) 4 STORE_NAME 0 (__doc__) | |
5 6 PUSH_NULL 5 6 PUSH_NULL 5 4 PUSH_NULL | |
8 LOAD_NAME 1 (print) 8 LOAD_NAME 1 (print) 6 LOAD_NAME 0 (print) | |
10 LOAD_NAME 0 (__doc__) 10 LOAD_NAME 0 (__doc__) 8 LOAD_NAME 1 (__doc__) | |
12 PRECALL 1 12 PRECALL 1 10 PRECALL 1 | |
16 CALL 1 16 CALL 1 14 CALL 1 | |
26 POP_TOP 26 POP_TOP 24 POP_TOP | |
7 28 NOP 7 28 NOP 7 26 NOP | |
8 30 PUSH_NULL | |
32 LOAD_NAME 1 (print) | |
34 LOAD_CONST 2 ('DEBUGGING') | |
36 PRECALL 1 | |
40 CALL 1 | |
50 POP_TOP | |
10 52 NOP 10 30 NOP 10 28 NOP | |
11 32 PUSH_NULL 11 30 PUSH_NULL | |
34 LOAD_NAME 1 (print) 32 LOAD_NAME 0 (print) | |
36 LOAD_CONST 4 ('OPTIMIZING') 34 LOAD_CONST 3 ('OPTIMIZING') | |
38 PRECALL 1 36 PRECALL 1 | |
42 CALL 1 40 CALL 1 | |
52 POP_TOP 50 POP_TOP | |
54 LOAD_CONST 5 (None) 52 LOAD_CONST 4 (None) | |
56 RETURN_VALUE 54 RETURN_VALUE | |
13 54 LOAD_ASSERTION_ERROR | |
56 LOAD_CONST 5 ('ASSERT') | |
58 PRECALL 0 | |
62 CALL 0 | |
72 RAISE_VARARGS 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment