Si consideri il seguente codice usando array NumPy che è molto lento:Accelerare un ciclo di Numpy in 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
come riscrivere la funzione di velocizzare il calcolo? (np.size(tx) == 10000
e np.size(ox) == 100000
)
Considerate anche l'utilizzo di OpenCL? –
Non ho bisogno di prestazioni complete, voglio solo una velocità raw. – Vincent
Costruisci un 'scipy.spatial.KDTree' dai punti tx, ty, tz e poi usa la ricerca del vicino più prossimo nella norma dell'infinito per ogni punto in bue, oy, oz per vedere se c'è qualche punto abbastanza vicino. –