memory allocation for Python list

Discussion in 'Python' started by dmitrey, Mar 26, 2008.

  1. dmitrey

    dmitrey Guest

    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
     
    dmitrey, Mar 26, 2008
    #1
    1. Advertising

  2. dmitrey

    Guest

    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, 2008
    #2
    1. Advertising

  3. dmitrey

    Guest

    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, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    720
    Eric Sosman
    Mar 22, 2005
  2. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    615
    Jean-Claude Arbaut
    Jun 15, 2005
  3. Ken
    Replies:
    24
    Views:
    3,896
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    1,007
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,207
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page