[Tutor] Double loop and performances
Benjamin Klein-Fignier
bkfignier at clsp.jhu.edu
Tue Jun 7 15:18:38 CEST 2005
Hi,
I am wondering if there is a way to efficiently run a double loop in
Python. To be more precise, I would like to compute the sum of a
function f(x,y) for every possible (x,y) pair.
I have tried several alternatives so far:
1) the classic double loop:
for x in X:
for y in Y:
sum += f(x,y)
2) using list comprehension:
sum([f(x,y) for x in X for y in Y])
3) I figured out that the loop was slow, so I used map() to generate
a list of every combination and then map to compute the result. But
it is not very memory efficient and still pretty slow.
I know I might try to use SciPy & co to do it but I would prefer to
be able to run my script on any default python installation. Any
suggestion ?
Regards,
-- Ben
More information about the Tutor
mailing list