4 ms·
Interesting article. I've always heard that about lists and tuples, but this article made me wonder how much less efficient with storage is a list. The follow
by ratsbane 10y ago
Interesting article. I've always heard that about lists and tuples, but this article made me wonder how much less efficient with storage is a list.
The following trivial example shows that, at least for very small lists and tuples, except for the fixed overhead of 16 bytes for a tuple vs. 72 bytes for a list they're the same [edit: no it doesn't. I wasn't looking at the diff between a 1-element and 2-element tuple]:
Perhaps there's more to it?
>>> from sys import getsizeof
>>> list = [94]
>>> getsizeof(list)
80
>>> list = [94, 37]
>>> getsizeof(list)
88
>>> list = [94, 37, 21]
>>> getsizeof(list)
96
>>> list = [94, 37, 21, 19]
>>> getsizeof(list)
104
>>> tuple = (94)
>>> getsizeof(tuple)
24
>>> tuple = (94, 37)
>>> getsizeof(tuple)
72
>>> tuple = (94, 37, 21)
>>> getsizeof(tuple)
80
>>> tuple = (94, 37, 21, 19)
>>> getsizeof(tuple)
88
- colanderman 10y ago(94) is not a tuple. (94,) is.
- ratsbane 10y agoD'oh! Thank you, colanderman. >>> tuple = (94,) >>> getsizeof(tuple) 64
- ratsbane 10y agoAnother (admittedly trivial) experiment with speed and storage: Iterating through a 10,000 element list was about 2% slower than iterating through a 10,000 element tuple, and the list was 16 bytes bigger than the tuple: #!/usr/bin/python from sys import getsizeof from random import sample from time import time l = sample(xrange(10000), 10000) t = tuple(l) print "The tuple's size is " + str(getsizeof(t)) + " and the list's size is " + str(getsizeof(l)) s = time() z = 0 for x in l: z += x print "Iterating through a 10,000 element list took " + str(time() - s) + " seconds." s = time() z = 0 for x in t: z += x print "Iterating through a 10,000 element tuple took " + str(time() - s) + " seconds." doug@supermicro:~$ ./x The tuple's size is 80056 and the list's size is 80072 Iterating through a 10,000 element list took 0.00589299201965 seconds. Iterating through a 10,000 element tuple took 0.00538778305054 seconds. doug@supermicro:~$ ./x The tuple's size is 80056 and the list's size is 80072 Iterating through a 10,000 element list took 0.00553607940674 seconds. Iterating through a 10,000 element tuple took 0.00536203384399 seconds.
- psyklic 10y agoThis code seems inconclusive -- my first three runs showed that lists are the same, slower, AND faster! (updated for Python 3.5.2 on Windows) C:\Temp>python listtuple.py The tuple's size is 80048 and the list's size is 80064 Iterating through a 10,000 element list took 0.0005006790161132812 seconds. Iterating through a 10,000 element tuple took 0.0005006790161132812 seconds. C:\Temp>python listtuple.py The tuple's size is 80048 and the list's size is 80064 Iterating through a 10,000 element list took 0.0020074844360351562 seconds. Iterating through a 10,000 element tuple took 0.001031637191772461 seconds. C:\Temp>python listtuple.py The tuple's size is 80048 and the list's size is 80064 Iterating through a 10,000 element list took 0.0 seconds. Iterating through a 10,000 element tuple took 0.001001596450805664 seconds.
- ratsbane 10y agoInteresting! The previous example I posted was Python 2.7.12 on Ubuntu 16.04.01. Here is OSX 10.11.5 w/Python 2.7.10 (also inconclusive with respect to speed, but OSX was half the memory): mb:~ doug$ ./listtuple The tuple's size is 40028 and the list's size is 40036 Iterating through a 10,000 element list took 0.00113916397095 seconds. Iterating through a 10,000 element tuple took 0.00111603736877 seconds. mb:~ doug$ ./listtuple The tuple's size is 40028 and the list's size is 40036 Iterating through a 10,000 element list took 0.00126791000366 seconds. Iterating through a 10,000 element tuple took 0.00133585929871 seconds. mb:~ doug$ ./listtuple The tuple's size is 40028 and the list's size is 40036 Iterating through a 10,000 element list took 0.00114607810974 seconds. Iterating through a 10,000 element tuple took 0.00117492675781 seconds.
- colanderman 10y agoIIRC lists and tuples are implemented nearly identically in CPython. So you won't find much difference here. Because Python is so dynamic, you can't make lists or tuples much better. However, in a static language (like Java or OCaml), you can greatly shrink the size of "cultural" lists by hoisting the (identical) run-time type info out of the elements and into the container. (OCaml does this with floats.) And "cultural" tuples typically can use much simpler implementations, since their sizes are fixed and known at compile time. (C structs are often optimized away entirely in optimized assembly.)
- ratsbane 10y agoI'm really surprised by these numbers. There isn't really any speed advantage or significant storage advantage to using tuples over lists. That really cuts down on the rationale for using tuples over lists, except for the "cultural difference" as discussed (and the ability to use lists as dictionary keys, which isn't something I think I've ever wanted to do)