到目前为止,我从来没有担心过这个问题,但现在我需要使用大量需要由 PyOpenGL 缓冲的顶点,而且似乎 python 迭代是瓶颈。这是情况。我有一个 3D 点数组vertices
,在每一步我都必须为每个顶点计算一个 4D 颜色数组。到目前为止,我的方法是:
upper_border = len(self.vertices) / 3
#Only generate at first step, otherwise use old one and replace values
if self.color_array is None:
self.color_array = numpy.empty(4 * upper_border)
for i in range(upper_border):
#Obtain a color between a start->end color
diff_activity = (activity[i] - self.min) / abs_diff
clr_idx = i * 4
self.color_array[clr_idx] = start_colors[0] + diff_activity * end_colors[0]
self.color_array[clr_idx + 1] = start_colors[1] + diff_activity * end_colors[1]
self.color_array[clr_idx + 2] = start_colors[2] + diff_activity * end_colors[2]
self.color_array[clr_idx + 3] = 1
现在我认为我不能做任何其他事情来消除循环的每个步骤中的操作,但我猜必须有一种更优化的性能方式来执行该循环。我之所以这么说是因为例如在 javascript 中,相同的演算会产生 9FPS,而在 Python 中我只能得到 2-3 FPS。
问候, 博格丹