468,301 Members | 1,396 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

memory allocation for Python list

hi all,
I have a python list of unknown length, that sequentially grows up via
adding single elements.
Each element has same size in memory (numpy.array of shape 1 x N, N is
known from the very beginning).
As I have mentioned, I don't know final length of the list, but
usually I know a good approximation, for example 400.

So, how can I optimize a code for the sake of calculations speedup?
Currently I just use

myList = []

for i in some_range:
...
myList.append(element)
...

Thank you in advance,
Dmitrey

Mar 26 '08 #1
2 2193
dmitrey:
As I have mentioned, I don't know final length of the list, but
usually I know a good approximation, for example 400.
There is no reserve()-like method, but this is a fast enough operation
you can do at the beginning:

l = [None] * 400

It may speed up your code, but the final resizing may kill your
performance anyway. You can try it. Just using Psyco is probably
better.

Bye,
bearophile
Mar 26 '08 #2
dmitrey:
As I have mentioned, I don't know final length of the list, but
usually I know a good approximation, for example 400.
You may want to use collections.deque too, it doesn't produce a Python
list, but it's quite fast in appending (it's a linked list of small
arrays).

Bye,
bearophile
Mar 26 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Avi Kak | last post: by
6 posts views Thread by benevilent | last post: by
9 posts views Thread by Chris S. | last post: by
10 posts views Thread by Steven D'Aprano | last post: by
13 posts views Thread by placid | last post: by
81 posts views Thread by Peter Olcott | last post: by
17 posts views Thread by frederic.pica | last post: by
5 posts views Thread by Santiago Romero | last post: by
reply views Thread by Calvin Spealman | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.