Re: question on list comprehensions

From: Darren Dale (dd55_at_cornell.edu)
Date: 10/14/04


Date: Thu, 14 Oct 2004 13:10:47 -0400

Carlos Ribeiro wrote:

> On Thu, 14 Oct 2004 11:16:12 -0400, Darren Dale <dd55@cornell.edu> wrote:
>> 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.
>
> Can't you just sum them inplace as you calculate every new array, for
> each particle? Something like this (pseudo code):
>
> result = make_empty_array()
> for particle in particles:
> result += make_complex_array(particle)
> return square(result)
>
> It does not grow the result array indefinitely.... so it solves your
> problem, if that's what you need/want to avoid.
>

You're example is what I have now, it's what I want to replace.

I am trying to make the operations general, so I can do 1x1, 1xN, and NxN
result arrays. In the case of NxN, the overhead in the for loop can be
small compared to the time required for the array operations. But for a 1x1
result array, the for loop is a huge time sink. I thought it would be nice
to get the speedup of the list comprehension, but see no way to change the
array in place. The list comprehension wants to create a new list instead.

At any rate, I found another way to frame my problem to get the speedup.
It's entirely based on linear algebra operations, no ingenious coding
involved, so I wont share the details. They are pretty mundane.



Relevant Pages

  • Re: Representing structures for simulations
    ... multidimensional array. ... cartesian grid; the grid can be distorted in various ways (e.g., ... Because the adjacency information is precomputed ... One simple method is storing, with each particle, a list of all of the ...
    (comp.lang.fortran)
  • Re: Optimization in gfortran
    ... optimisation leading to an infinite loop) but this could be a bug in your code too. ... is periodic) with a costant mesure, I wait they reach the asyntotic mesure and the I pass throw the integrator all the particle at the same time: for t=1 I integrate all the particle to t=1.01 and so on. ... In the secondo I put a single particle, store the relevant quantity in an array, every x time I do a cut and I recall the particle as the second particle and so on. ...
    (comp.lang.fortran)
  • Re: question on list comprehensions
    ... > I am simulating diffraction from an array of particles. ... > a complex array for each particle, add up all these arrays, and square the ... blog: http://rascunhosrotos.blogspot.com ...
    (comp.lang.python)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Kevin Backmann ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)