[Python-ideas] Length hinting and preallocation for container types

Alex Stewart foogod at gmail.com
Tue Mar 5 23:03:20 CET 2013

On Tue, Mar 5, 2013 at 12:42 PM, Andrew Barnert <abarnert at yahoo.com> wrote:

> So, that's a 1.8% speedup. While doing things right gives a 20% speedup:
> $ python3.3 -m timeit -n1000 "l=[]" "app = l.append" "for i in
> range(10000): app(i)"
> 1000 loops, best of 3: 557 usec per loop
> $ python3.3 -m timeit -n1000 "l = [i for i in range(10000)]"
> 1000 loops, best of 3: 447 usec per loop

Yeah, I think it's pretty likely that any case where the proposed change
would be applicable is probably going to be dominated by other factors more
than by allocation overhead anyway..

The above does beg the question, though:  Could we perhaps apply some of
this thinking to list comprehensions, and speed them up even more by
automatically taking hints from the inputs and preallocating the output
list? (or do we already?)

Not sure how much work that would be, or whether it would be worth it..
just a random thought..

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130305/3fae3732/attachment.html>

More information about the Python-ideas mailing list