这是一个循环,用于提取两个图像的RGB值,并计算所有三个通道的平方差之和。 直接在我的主.py需要0.07秒。如果我在这个.pyx文件中运行它,速度会降低到1秒。我读过关于cdef函数的文章,但是我没有成功地传递数组。如能帮助您将此函数转换为cdef函数,我们将不胜感激。我真的需要这个循环尽快进行。在
from cpython cimport array
import array
import numpy as np
cimport numpy as np
def fittnes(Orginal, Mutated):
Fittnes = 0
for x in range(0, 299):
for y in range(0, 299):
DeltaRed = (Orginal[x][y][0] - Mutated[x][y][0])
DeltaGreen = (Orginal[x][y][1] - Mutated[x][y][1])
DeltaBlue = (Orginal[x][y][2] - Mutated[x][y][2])
Fittnes += (DeltaRed * DeltaRed + DeltaGreen * DeltaGreen + DeltaBlue * DeltaBlue)
return Fittnes
我的主.py函数调用
^{pr2}$
让我有兴趣知道加速数字,所以我把这个作为一个解决方案。因此,正如注释中所述/讨论的,如果输入是NumPy数组,那么可以使用本机NumPy工具,在本例中是^{} ,如下-
您还可以将非常高效的^{} 用于相同的任务,如-
^{pr2}$运行时测试
定义函数-
时间安排-
相关问题 更多 >
编程相关推荐