[Python-ideas] Length hinting and preallocation for container types
Eli Bendersky
eliben at gmail.com
Fri Mar 8 15:43:47 CET 2013
On Fri, Mar 8, 2013 at 6:40 AM, Eli Bendersky <eliben at gmail.com> wrote:
> On Fri, Mar 8, 2013 at 6:04 AM, Daniel Holth <dholth at gmail.com> wrote:
>
>> On Fri, Mar 8, 2013 at 8:35 AM, Antoine Pitrou <solipsis at pitrou.net>
>> wrote:
>> > Le Fri, 8 Mar 2013 08:18:07 -0500,
>> > Daniel Holth <dholth at gmail.com> a écrit :
>> >>
>> >> I am a fan of the proposal. Imagine you are programming for a
>> >> memory-constrained system. By telling the list how big it needs to be
>> >> you can save precious RAM.
>> >
>> > Is it an actual use case or are you just imagining it? :)
>> > I'm asking because, unless you are only allocating that list and all
>> > the objects contained it in it already exist, limiting the list's size
>> > won't do much for the process' memory occupation.
>>
>> It might help if it was a list of integers between -1 and 99 and
>> 1-character strings.
>
>
> That's not what you should use lists for if memory consumption matters.
> Use http://docs.python.org/dev/library/array.html, especially if your
> integers are in such a limited range.
>
>>> sys.getsizeof(list(range(100)))
1024
>>> sys.getsizeof(array('i', list(range(100))))
480
>>> sys.getsizeof(array('b', list(range(100))))
180
This can help you *way* more than playing with growing and shrinking lists.
Eli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130308/08e8653a/attachment.html>
More information about the Python-ideas
mailing list