Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67391

Re: Can tuples be replaced with lists all the time?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Can tuples be replaced with lists all the time?
Date 2014-03-01 18:15 -0500
References <64af70e3-6876-4fbf-8386-330d2f48735a@googlegroups.com> <led9s7$req$1@reader1.panix.com> <roy-099864.12485223022014@news.panix.com> <letiu0$hfj$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.7549.1393715735.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Can tuples be replaced with lists all the time? Sam <lightaiyee@gmail.com> - 2014-02-22 20:06 -0800
  Re: Can tuples be replaced with lists all the time? Paul Rubin <no.email@nospam.invalid> - 2014-02-22 20:28 -0800
  Re: Can tuples be replaced with lists all the time? Chris Angelico <rosuav@gmail.com> - 2014-02-23 15:18 +1100
  Re: Can tuples be replaced with lists all the time? Ben Finney <ben+python@benfinney.id.au> - 2014-02-23 15:49 +1100
  Re: Can tuples be replaced with lists all the time? 88888 Dihedral <dihedral88888@gmail.com> - 2014-02-23 11:45 -0800
  Re: Can tuples be replaced with lists all the time? Roy Smith <roy@panix.com> - 2014-02-22 23:19 -0500
    Re: Can tuples be replaced with lists all the time? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-03-02 17:21 -0500
  Re: Can tuples be replaced with lists all the time? Grant Edwards <invalid@invalid.invalid> - 2014-02-23 17:07 +0000
    Re: Can tuples be replaced with lists all the time? Roy Smith <roy@panix.com> - 2014-02-23 12:48 -0500
      Re: Can tuples be replaced with lists all the time? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-01 21:20 +0000
      Re: Can tuples be replaced with lists all the time? Terry Reedy <tjreedy@udel.edu> - 2014-03-01 18:15 -0500

csiph-web