mardi 31 mars 2020

Is there a way to avoid the for-loops to boost my code in python?

I need some help to boost my performance. I got a given volume, which is a 3d-matrix out of 0s and 1s, representing the voxels. A also got a List of points. For every voxel in the volume I would like to know the Distance to the closest point contained in the list. The following code can be very time consuming.

for index, voxel in np.ndenumerate(Volume):

    if voxel == 1:    

            for index2 in ListofPoints:
                distance = np.sqrt((index[0]-index2[0])**2+(index[1]-index2[1])**2+(index[2]-index2[2])**2)
                distances.append(distance)

            Result_Distances[index] = np.amin(distances)

This takes about 1 Minute to compute, because my Volume can be very big (>1000 voxels). My ListofPoints is smaller (about 100 points).

Is there a smart way I can avoid the time consuming for loops? I also tried the map() function, but couldn't implement it in a way, so there is improvement.

Thank you for your help!

Aucun commentaire:

Enregistrer un commentaire