Most efficient way to "pre-grow" a list?

gil_johnson x7-g5W_rt at earthlink.net
Tue Nov 10 08:49:03 EST 2009


On Nov 9, 10:56 pm, "Gabriel Genellina" wrote:
>
> [much cutting]
>
> def method3a():
>    newArray = array.array('I', [INITIAL_VALUE]) * SIZE
>    assert len(newArray)==SIZE
>    assert newArray[SIZE-1]==INITIAL_VALUE
>
> [more cutting]
>
> So arrays are faster than lists, and in both cases one_item*N outperforms  
> your doubling algorithm.
> Adding one item at a time is -at least- several hundred times slower; I  
> was not patient enough to wait.
>
> > Finally, I just looked into calling C functions, and found
> > PyMem_Malloc, PyMem_Realloc, PyMem_Free, etc. in the Memory Management
> > section of the Python/C API Reference Manual. This gives you
> > uninitialized memory, and should be really fast, but it's 6:45 AM
> > here, and I don't have the energy to try it.
>
> No, those are for internal use only, you can't use the resulting pointer  
> in Python code. An array object is a contiguous memory block, so you don't  
> miss anything.
>

Gabriel,
Thanks for the reply - your method 3a was what I wanted,
I missed "array.array('I', [INITIAL_VALUE]) * SIZ" on my
earlier pass through the Python documentation. I included
the 'one at a time' method because that was my first shot
at the problem - I was so happy to find a method that would
get the job done today that I didn't look farther than the
doubling method.

Yes, I know that I was comparing lists and arrays. Part of the
confusion in this thread is that the problem hasn't been
defined very well. The original post asked about arrays
but the solution proposed generated a list. At this point,
I have learned a lot about arrays, lists and dictionaries, and
will be better able to chose the right solution to future
problems.

It's too bad about the C functions, they sure sounded good.

And with that, I think I will withdraw from the field, and go
write some code and learn about timeit.

Gil



More information about the Python-list mailing list