On 3/1/2014 4:20 PM, Mark Lawrence wrote:
On 23/02/2014 17:48, Roy Smith wrote:

It also appears that tuples are more memory efficient.  I just ran some
quick tests on my OSX box.  Creating a list of 10 million [1, 2, 3, 4,
5] lists gave me a 1445 MB process.   The name number of (1, 2, 3, 4, 5)
tuples was 748 MB.  I'm sure this is implementation dependent, but it
seems plausible to assume similar results will be had on other
implementations.

The numbers sound right.

In CPython a list is overallocated so there's usually spare slots
available if you want to add something to it.  In contrast you know when
you create the tuple just how big it is so no overallocation is needed.

Besides which, in CPython, a tuple is one block of memory, with a PyObject header followed by the object references, while a list uses 2 blocks of memory. The first has a PyObject header, followed by a reference to the list block, its allocated length (minimum 8 I believe), and its current in-use length. The over-allocated list block, which must also start on a 4 or 8 byte alignment boundary, has the object references plus extra space.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to