Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. >>> def a():
  2. ... x=[1,2,3,4,5]
  3. ... y=x[2]
  4. ...
  5. >>> def b():
  6. ... x=(1,2,3,4,5)
  7. ... y=x[2]
  8. ...
  9. >>> import dis
  10. >>> dis.dis(a)
  11. 2 0 LOAD_CONST 1 (1)
  12. 3 LOAD_CONST 2 (2)
  13. 6 LOAD_CONST 3 (3)
  14. 9 LOAD_CONST 4 (4)
  15. 12 LOAD_CONST 5 (5)
  16. 15 BUILD_LIST 5
  17. 18 STORE_FAST 0 (x)
  18.  
  19. 3 21 LOAD_FAST 0 (x)
  20. 24 LOAD_CONST 2 (2)
  21. 27 BINARY_SUBSCR
  22. 28 STORE_FAST 1 (y)
  23. 31 LOAD_CONST 0 (None)
  24. 34 RETURN_VALUE
  25. >>> dis.dis(b)
  26. 2 0 LOAD_CONST 6 ((1, 2, 3, 4, 5))
  27. 3 STORE_FAST 0 (x)
  28.  
  29. 3 6 LOAD_FAST 0 (x)
  30. 9 LOAD_CONST 2 (2)
  31. 12 BINARY_SUBSCR
  32. 13 STORE_FAST 1 (y)
  33. 16 LOAD_CONST 0 (None)
  34. 19 RETURN_VALUE
  35.  
  36. $ python -m timeit "x=(1,2,3,4,5,6,7,8)"
  37. 10000000 loops, best of 3: 0.0388 usec per loop
  38.  
  39. $ python -m timeit "x=[1,2,3,4,5,6,7,8]"
  40. 1000000 loops, best of 3: 0.363 usec per loop
  41.  
  42. $ python -m timeit -s "x=(1,2,3,4,5,6,7,8)" "y=x[3]"
  43. 10000000 loops, best of 3: 0.0938 usec per loop
  44.  
  45. $ python -m timeit -s "x=[1,2,3,4,5,6,7,8]" "y=x[3]"
  46. 10000000 loops, best of 3: 0.0649 usec per loop
  47.  
  48. >>> from dis import dis
  49.  
  50. >>> dis(compile("(10, 'abc')", '', 'eval'))
  51. 1 0 LOAD_CONST 2 ((10, 'abc'))
  52. 3 RETURN_VALUE
  53.  
  54. >>> dis(compile("[10, 'abc']", '', 'eval'))
  55. 1 0 LOAD_CONST 0 (10)
  56. 3 LOAD_CONST 1 ('abc')
  57. 6 BUILD_LIST 2
  58. 9 RETURN_VALUE
  59.  
  60. typedef struct {
  61. Py_ssize_t ob_refcnt;
  62. struct _typeobject *ob_type;
  63. Py_ssize_t ob_size;
  64. PyObject *ob_item[2]; /* store a pointer to 10 and a pointer to 20 */
  65. } PyTupleObject;
  66.  
  67. PyObject arr[2]; /* store a pointer to 10 and a pointer to 20 */
  68.  
  69. typedef struct {
  70. Py_ssize_t ob_refcnt;
  71. struct _typeobject *ob_type;
  72. Py_ssize_t ob_size;
  73. PyObject **ob_item = arr; /* store a pointer to the two-pointer array */
  74. Py_ssize_t allocated;
  75. } PyListObject;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement