Consultez le code suivant à l'aide des tableaux numpy qui est très lent:Accélérer une boucle numpy en python?
# Intersection of an octree and a trajectory
def intersection(octree, trajectory):
# Initialize numpy arrays
ox = octree.get("x")
oy = octree.get("y")
oz = octree.get("z")
oe = octree.get("extent")/2
tx = trajectory.get("x")
ty = trajectory.get("y")
tz = trajectory.get("z")
result = np.zeros(np.size(ox))
# Loop over elements
for i in range(0, np.size(tx)):
for j in range(0, np.size(ox)):
if (tx[i] > ox[j]-oe[j] and
tx[i] < ox[j]+oe[j] and
ty[i] > oy[j]-oe[j] and
ty[i] < oy[j]+oe[j] and
tz[i] > oz[j]-oe[j] and
tz[i] < oz[j]+oe[j]):
result[j] += 1
# Finalize
return result
Comment réécrire la fonction pour accélérer le calcul? (np.size(tx) == 10000
et np.size(ox) == 100000
)
Envisagez-vous également d'utiliser OpenCL? –
Je n'ai pas besoin de performances complètes, je veux juste une accélération brute. – Vincent
Construire un 'scipy.spatial.KDTree' à partir des points tx, ty, tz et ensuite utiliser la recherche du plus proche voisin dans la norme infinity pour chaque point dans ox, oy, oz pour voir s'il y a un point assez proche. –