question on list comprehensions

Josiah Carlson jcarlson at uci.edu
Thu Oct 14 13:17:11 EDT 2004


> OK. As usual, I am having trouble clearly expressing myself. Sorry about
> that. Prepare for some physics:
> 
> I am simulating diffraction from an array of particles. I have to calculate
> a complex array for each particle, add up all these arrays, and square the
> magnitude of the result. If I do a for loop, it takes about 6-8 seconds for
> a 2000 element array added up over 250 particles. In reality, I will have
> 2500 particles, or even 2500x2500 particles. 
> 
> The list comprehension takes only 1.5 seconds for 250 particles. Already,
> that means the time has decreased from 40 hours to 10, and that time can be
> reduced further if python is not constantly requesting additionaly memory
> to grow the resulting list. 
> 
> I guess that means that I would like to avoid growing the list and popping
> the previous result if possible, and just over-write the previous result.

Download scientific python: http://www.scipy.org/
And get your linear algebra on.  Using Numeric arrays, the size of the
matrices (because they are matrices) are much smaller than the size of
an equivalent Python list of lists, so memory may stop being a concern.

 - Josiah




More information about the Python-list mailing list