我从来没有关心过这个问题,但现在我需要使用一些需要被PyOpenGL缓冲的大量顶点,而且好像python迭代是瓶颈。这是情况。我有一个3D点阵列vertices
,并且在每一步我都必须为每个顶点计算一个4D的颜色数组。我的做法迄今:在python中迭代的最快方法
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。
问候, 波格丹
如果您使用python 2,请用'xrange'替换'range'。 –
如果迭代确实是瓶颈,那么有一种可能性是在Cython中重新编码循环:http://cython.org/ – NPE