Created
August 16, 2019 23:42
-
-
Save juanarrivillaga/829cd481bad0044da933cd18c202ce9b to your computer and use it in GitHub Desktop.
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
>>> import dis | |
>>> def regular_loop(source): | |
... result = [] | |
... for x in result: | |
... result.append(x**2) | |
... return result | |
... | |
>>> def list_comp(source): | |
... result = [x**2 for x in source] | |
... return result | |
... | |
>>> dis.dis(regular_loop) | |
2 0 BUILD_LIST 0 | |
2 STORE_FAST 1 (result) | |
3 4 SETUP_LOOP 26 (to 32) | |
6 LOAD_FAST 1 (result) | |
8 GET_ITER | |
>> 10 FOR_ITER 18 (to 30) | |
12 STORE_FAST 2 (x) | |
4 14 LOAD_FAST 1 (result) | |
16 LOAD_METHOD 0 (append) | |
18 LOAD_FAST 2 (x) | |
20 LOAD_CONST 1 (2) | |
22 BINARY_POWER | |
24 CALL_METHOD 1 | |
26 POP_TOP | |
28 JUMP_ABSOLUTE 10 | |
>> 30 POP_BLOCK | |
5 >> 32 LOAD_FAST 1 (result) | |
34 RETURN_VALUE | |
>>> dis.dis(list_comp) | |
2 0 LOAD_CONST 1 (<code object <listcomp> at 0x10d69f0c0, file "<stdin>", line 2>) | |
2 LOAD_CONST 2 ('list_comp.<locals>.<listcomp>') | |
4 MAKE_FUNCTION 0 | |
6 LOAD_FAST 0 (source) | |
8 GET_ITER | |
10 CALL_FUNCTION 1 | |
12 STORE_FAST 1 (result) | |
3 14 LOAD_FAST 1 (result) | |
16 RETURN_VALUE | |
Disassembly of <code object <listcomp> at 0x10d69f0c0, file "<stdin>", line 2>: | |
2 0 BUILD_LIST 0 | |
2 LOAD_FAST 0 (.0) | |
>> 4 FOR_ITER 12 (to 18) | |
6 STORE_FAST 1 (x) | |
8 LOAD_FAST 1 (x) | |
10 LOAD_CONST 0 (2) | |
12 BINARY_POWER | |
14 LIST_APPEND 2 | |
16 JUMP_ABSOLUTE 4 | |
>> 18 RETURN_VALUE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment