zoukankan      html  css  js  c++  java
  • Are tuples more efficient than lists in Python?

          在stackoverflow上看到的一篇,刚好解答了我的疑惑:http://stackoverflow.com/questions/68630/are-tuples-more-efficient-than-lists-in-python

    The "dis" module disassembles the byte code for a function and is useful to see the difference between tuples and lists.

    In this case, you can see that accessing an element generates identical code, but that assigning a tuple is much faster than assigning a list.

    >>> 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

    In general, you might expect tuples to be slightly faster. However you should definitely test your specific case (if the difference might impact the performance of your program -- remember "premature optimization is the root of all evil").

    Python makes this very easy: timeit is your friend.

    $ 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

    and...

    $ 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

    So in this case, instantiation is almost an order of magnitude faster for the tuple, but item access is actually somewhat faster for the list! So if you're creating a few tuples and accessing them many many times, it may actually be faster to use lists instead.

    Of course if you want to change an item, the list will definitely be faster since you'd need to create an entire new tuple to change one item of it (since tuples are immutable).

  • 相关阅读:
    iOS小技巧总结,绝对有你想要的
    Myeclipse for Mac快捷键
    iOS开发如何学习前端
    iOS应用支持IPV6,就那点事儿
    App Store审核被拒的23个理由
    43个优秀的Swift开源项目
    ExtJs组件之间的相互访问,访问机制
    hibernate or连接查询内容/criteria动态或连接查询/disjunction/其他查询条件
    hibernate如何解除关系———只删除多方保留一方
    java如何操作视图
  • 原文地址:https://www.cnblogs.com/codingmylife/p/2830820.html
Copyright © 2011-2022 走看看