Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with 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.
Here is another little benchmark, just for the sake of it..
Let's average these out:
You can call it almost inconclusive.
But sure, tuples took
101.239%
the time, or1.239%
extra time to do the job compared to lists.Summary
Tuples tend to perform better than lists in almost every category:
1) Tuples can be constant folded.
2) Tuples can be reused instead of copied.
3) Tuples are compact and don't over-allocate.
4) Tuples directly reference their elements.
Tuples can be constant folded
Tuples of constants can be precomputed by Python's peephole optimizer or AST-optimizer. Lists, on the other hand, get built-up from scratch:
Tuples do not need to be copied
Running
tuple(some_tuple)
returns immediately itself. Since tuples are immutable, they do not have to be copied:In contrast,
list(some_list)
requires all the data to be copied to a new list:Tuples do not over-allocate
Since a tuple's size is fixed, it can be stored more compactly than lists which need to over-allocate to make append() operations efficient.
This gives tuples a nice space advantage:
Here is the comment from Objects/listobject.c that explains what lists are doing:
Tuples refer directly to their elements
References to objects are incorporated directly in a tuple object. In contrast, lists have an extra layer of indirection to an external array of pointers.
This gives tuples a small speed advantage for indexed lookups and unpacking:
Here is how the tuple
(10, 20)
is stored:Here is how the list
[10, 20]
is stored:Note that the tuple object incorporates the two data pointers directly while the list object has an additional layer of indirection to an external array holding the two data pointers.
You should also consider the
array
module in the standard library if all the items in your list or tuple are of the same C type. It will take less memory and can be faster.Tuples, being immutable, are more memory efficient; lists, for efficiency, overallocate memory in order to allow appends without constant
realloc
s. So, if you want to iterate through a constant sequence of values in your code (egfor direction in 'up', 'right', 'down', 'left':
), tuples are preferred, since such tuples are pre-calculated in compile time.Access speeds should be the same (they are both stored as contiguous arrays in the memory).
But,
alist.append(item)
is much preferred toatuple+= (item,)
when you deal with mutable data. Remember, tuples are intended to be treated as records without field names.Tuples should be slightly more efficient and because of that, faster, than lists because they are immutable.