How to make this speed up

myang myang at clarku.edu
Tue Mar 30 16:54:18 EST 2004


Hi,

I have thousands of points in 3D space, and I want to calcuate the distance
between each other of them. I do the calculation with the following code.

def distance(a,b):
    sum = 0
    for i in range(len(a)):
        sum += (a[i]-b[i])**2
    return sqrt(sum)

for i in range(len(plist)):
    for j in range(i+1,len(plist)):
        d = distance(plist[i],plist[j])

But this is damn slow. Do you have any suggestion to speed it up? If I write
the distance function in C extension, how faster will it be? (For the time
being, I don't know how to write C extension yet :(

Regards,
Yang





More information about the Python-list mailing list