[Tutor] Acceleration of long lists

Peter Otten __peter__ at web.de
Fri Jul 30 03:44:10 EDT 2021


On 29/07/2021 20:02, Jan Kenin wrote:

> Often I append instances of objects in list-objects. Then I realize that
> the progress of my program slows down, when the list becomes larger and
> larger, e.g. larger than 3000. How can I accerlerate this? Perhaps it is
> a question of memory. Can I initialize the list in order to do the
> memory allocation only once?

You can initialize the list at once, but not the objects in the list.
However, the effect of that optimization is negligible, even for lists 
that are much longer than 3000 items:

PS C:\Users\Peter> py -m timeit  "a = []
 >> for i in range(10**6): a.append(i)"
1 loop, best of 5: 430 msec per loop

PS C:\Users\Peter> py -m timeit "a = [None] * 10**6
 >> for i, k in enumerate(range(10**6)): a[i] = k"
1 loop, best of 5: 442 msec per loop

> Thanks for all ideas!

If you are seeing a user-perceivable slowdown for a small list of 
only(!) 3000 items there must be another problem than the one you suspected.

Can you provide an example with more details? If it's not too long some 
actual code would be nice.



More information about the Tutor mailing list