<p>您的代码有一些对性能有很大影响的问题(例如,不使用np.附加在循环中)。
因此,第一步是改进代码,同时避免不必要的搜索。你知道吗</p>
<p>在下一步中,我们可以使用jit编译器来获得额外的性能。你知道吗</p>
<p><strong>代码</strong></p>
<pre><code>import numpy as np
import numba as nb
import time
from scipy import spatial
N_verts=350000
#This gernerates a mesh for performance Testing
#https://stackoverflow.com/a/50579387/4045774
def make(N):
verts= np.random.uniform(-10, 10, (N, 3))
faces = spatial.Delaunay(verts[:, :2]).simplices
return verts,faces
@nb.njit()
def face2vertVal(faces, facesVals):
# INPUT:
# faces: Nx3 array of N vertex IDs for N faces
# facesVals: Nx1 array of some parameter for each face
# OUTPUT:
# vertsVals: Px1 array of the mean value in "facesVals" that corresponds
# to each vertex
vertsVals = np.zeros(faces.max()+1)
verts_counts= np.zeros(faces.max()+1)
for i in range(faces.shape[0]):
for j in range(faces.shape[1]):
vertsVals[faces[i,j]]+=facesVals[i]
verts_counts[faces[i,j]]+= 1.
vertsVals=vertsVals/verts_counts
return vertsVals
[verts,faces]=make(N_verts)
facesVals=np.random.rand(faces.shape[0])
res=face2vertVal(faces, facesVals)
</code></pre>
<p><strong>性能</strong></p>
<pre><code>N_verts=350 000
Pure-Python:2.12s
With Numba (after the compilation which takes about 0.5s at the first call): 18.7ms
</code></pre>