[Python-ideas] Length hinting and preallocation for container types
Christian Heimes
christian at python.org
Wed Mar 6 12:47:20 CET 2013
Am 05.03.2013 21:42, schrieb Andrew Barnert:
> 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
>
> Or (but obviously this isn't generally applicable):
Obviously a list comprehension can't be used in all cases, too. AFAIK
the list comprehension already pre-allocates 10000 slots because a range
object has a length.
More information about the Python-ideas
mailing list