Created
April 23, 2013 16:54
-
-
Save anonymous/5445383 to your computer and use it in GitHub Desktop.
Compare two list building methods
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 a(): | |
... return [ x for x in some_list if x % 2 == 0 ] | |
... | |
>>> dis.dis(a) | |
2 0 BUILD_LIST 0 | |
3 LOAD_GLOBAL 0 (some_list) | |
6 GET_ITER | |
>> 7 FOR_ITER 28 (to 38) | |
10 STORE_FAST 0 (x) | |
13 LOAD_FAST 0 (x) | |
16 LOAD_CONST 1 (2) | |
19 BINARY_MODULO | |
20 LOAD_CONST 2 (0) | |
23 COMPARE_OP 2 (==) | |
26 POP_JUMP_IF_FALSE 7 | |
29 LOAD_FAST 0 (x) | |
32 LIST_APPEND 2 | |
35 JUMP_ABSOLUTE 7 | |
>> 38 RETURN_VALUE | |
>>> def b(): | |
... list=[] | |
... for x in some_list: | |
... if x%2==O: | |
... list.append(x) | |
... return list | |
... | |
>>> dis.dis(b) | |
2 0 BUILD_LIST 0 | |
3 STORE_FAST 0 (list) | |
3 6 SETUP_LOOP 46 (to 55) | |
9 LOAD_GLOBAL 0 (some_list) | |
12 GET_ITER | |
>> 13 FOR_ITER 38 (to 54) | |
16 STORE_FAST 1 (x) | |
4 19 LOAD_FAST 1 (x) | |
22 LOAD_CONST 1 (2) | |
25 BINARY_MODULO | |
26 LOAD_GLOBAL 1 (O) | |
29 COMPARE_OP 2 (==) | |
32 POP_JUMP_IF_FALSE 13 | |
5 35 LOAD_FAST 0 (list) | |
38 LOAD_ATTR 2 (append) | |
41 LOAD_FAST 1 (x) | |
44 CALL_FUNCTION 1 | |
47 POP_TOP | |
48 JUMP_ABSOLUTE 13 | |
51 JUMP_ABSOLUTE 13 | |
>> 54 POP_BLOCK | |
6 >> 55 LOAD_FAST 0 (list) | |
58 RETURN_VALUE | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment