Is there a performance difference between Tuple and List?

Asked

Viewed 223 times

4

In Python, I know there’s a difference between Tuple and a List. To Tuple is immutable, and the List, mutable.

You even have that question here: What is the main difference between a Tuple and a List?

However, I would like to know if, in terms of performance, one can be better than the other.

The Tuple, because it is immutable, it can generate more performance/performance than if it were to use a List?

2 answers

4


Tuples are simpler because they are immutable and for other reasons.

In the ONLY ONE ANSWER of Mark Harrison which already shows how the bytecode Python is mounted in each case and you can see that Tuple is more efficient.

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

To reply of Alex Martelli, who is an expert in Python, in another question says that tuples are built faster.

$ python3.1 -mtimeit -s'x,y,z=1,2,3' '[x,y,z]'
1000000 loops, best of 3: 0.379 usec per loop
$ python3.1 -mtimeit '[1,2,3]'
1000000 loops, best of 3: 0.413 usec per loop

$ python3.1 -mtimeit -s'x,y,z=1,2,3' '(x,y,z)'
10000000 loops, best of 3: 0.174 usec per loop
$ python3.1 -mtimeit '(1,2,3)'
10000000 loops, best of 3: 0.0602 usec per loop

$ python2.6 -mtimeit -s'x,y,z=1,2,3' '[x,y,z]'
1000000 loops, best of 3: 0.352 usec per loop
$ python2.6 -mtimeit '[1,2,3]'
1000000 loops, best of 3: 0.358 usec per loop

$ python2.6 -mtimeit -s'x,y,z=1,2,3' '(x,y,z)'
10000000 loops, best of 3: 0.157 usec per loop
$ python2.6 -mtimeit '(1,2,3)'
10000000 loops, best of 3: 0.0527 usec per loop

The fact that they are immutable gives the right of the codes that access it does not need to do certain checks, there are some more guarantees. This saves time.

There’s another response in the OS of Glenn Maynard which shows how Python internally manipulates the list:

case BINARY_SUBSCR:
    w = POP();
    v = TOP();
    if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
        /* INLINE: list[int] */
        Py_ssize_t i = PyInt_AsSsize_t(w);
        if (i < 0)
            i += PyList_GET_SIZE(v);
        if (i >= 0 && i < PyList_GET_SIZE(v)) {
            x = PyList_GET_ITEM(v, i);
            Py_INCREF(x);
        }

I put in the Github for future reference.

This makes access, which is what matters most, practically the same thing.

Tuples also take up less space, which can help in data caching.

There may be other implications. Each situation may have a different effect. But the question is unclear as to what situation you are talking about.

The conclusion is the same as always. Use what is most suitable for each situation and do not worry about performance. In this case, as in many, the difference is minimal and if you really want performance, do it in C, C++ or another language where the performance can be the maximum.

0

Tuples are stored in a single memory block. Tuples are immutable, so do not require additional space to store new objects.

Lists are allocated in two blocks, one fixed with all information from python objects and a variable space block for the data.

This is the reason why tuples are more performative.

It also explains why the small difference in the speed of indexing that in the lists, because the tuples for indexing follow less pointers.

reference: learnbatta

Browser other questions tagged

You are not signed in. Login or sign up in order to post.