Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> def a():
- ... x=[1,2,3,4,5]
- ... y=x[2]
- ...
- >>> def b():
- ... x=(1,2,3,4,5)
- ... y=x[2]
- ...
- >>> import dis
- >>> dis.dis(a)
- 2 0 LOAD_CONST 1 (1)
- 3 LOAD_CONST 2 (2)
- 6 LOAD_CONST 3 (3)
- 9 LOAD_CONST 4 (4)
- 12 LOAD_CONST 5 (5)
- 15 BUILD_LIST 5
- 18 STORE_FAST 0 (x)
- 3 21 LOAD_FAST 0 (x)
- 24 LOAD_CONST 2 (2)
- 27 BINARY_SUBSCR
- 28 STORE_FAST 1 (y)
- 31 LOAD_CONST 0 (None)
- 34 RETURN_VALUE
- >>> dis.dis(b)
- 2 0 LOAD_CONST 6 ((1, 2, 3, 4, 5))
- 3 STORE_FAST 0 (x)
- 3 6 LOAD_FAST 0 (x)
- 9 LOAD_CONST 2 (2)
- 12 BINARY_SUBSCR
- 13 STORE_FAST 1 (y)
- 16 LOAD_CONST 0 (None)
- 19 RETURN_VALUE
- $ python -m timeit "x=(1,2,3,4,5,6,7,8)"
- 10000000 loops, best of 3: 0.0388 usec per loop
- $ python -m timeit "x=[1,2,3,4,5,6,7,8]"
- 1000000 loops, best of 3: 0.363 usec per loop
- $ python -m timeit -s "x=(1,2,3,4,5,6,7,8)" "y=x[3]"
- 10000000 loops, best of 3: 0.0938 usec per loop
- $ python -m timeit -s "x=[1,2,3,4,5,6,7,8]" "y=x[3]"
- 10000000 loops, best of 3: 0.0649 usec per loop
- >>> from dis import dis
- >>> dis(compile("(10, 'abc')", '', 'eval'))
- 1 0 LOAD_CONST 2 ((10, 'abc'))
- 3 RETURN_VALUE
- >>> dis(compile("[10, 'abc']", '', 'eval'))
- 1 0 LOAD_CONST 0 (10)
- 3 LOAD_CONST 1 ('abc')
- 6 BUILD_LIST 2
- 9 RETURN_VALUE
- typedef struct {
- Py_ssize_t ob_refcnt;
- struct _typeobject *ob_type;
- Py_ssize_t ob_size;
- PyObject *ob_item[2]; /* store a pointer to 10 and a pointer to 20 */
- } PyTupleObject;
- PyObject arr[2]; /* store a pointer to 10 and a pointer to 20 */
- typedef struct {
- Py_ssize_t ob_refcnt;
- struct _typeobject *ob_type;
- Py_ssize_t ob_size;
- PyObject **ob_item = arr; /* store a pointer to the two-pointer array */
- Py_ssize_t allocated;
- } PyListObject;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement