Vic*_*man 2 python optimization cython
我们有一些用python编写的代码,它使用的几个类实际上只是"结构" - 这些类的实例只包含一堆字段而没有方法.例:
class ResProperties:
def __init__(self):
self.endDayUtilities = 0
self.marginalUtilities = []
self.held = 0
self.idleResource = True
self.experience = 0.0
self.resSetAside = 0
self.unitsGatheredToday = 0
Run Code Online (Sandbox Code Playgroud)
我们的主要代码使用了这个类的一堆实例.
为了加快代码速度,我想我已经对这个类进行了cython-ized:
cdef class ResProperties:
cdef public float endDayUtilities
cdef public list marginalUtilities
cdef public int held
cdef public int idleResource
cdef public float experience
cdef public int resSetAside
cdef public int unitsGatheredToday
def __init__(self):
self.endDayUtilities = 0
# etc: code just like above.
Run Code Online (Sandbox Code Playgroud)
但是,结果是代码现在运行速度慢了25%!
我如何找出导致代码运行速度变慢的原因?
谢谢.
您将这些类转换为Cython但仍在Python代码中使用它们?
将数据从C转换为Python并返回将导致开销.例如,您的endDayUtilities
成员是C风格的浮动.当您从Python访问它时,float()
必须在Python代码可以对其执行任何操作之前构造对象.当您从Python分配该属性时,同样的事情必须反过来发生.
在我的头脑中,我估计这些数据转换的性能开销为......哦,大约25%.:-)
在将使用该数据的一些代码移动到Cython 之前,您不会看到性能提升.基本上,你可以留在C-land越多,你就会越好.来回走动会杀了你.
作为另一种更简单的方法,您可能想要尝试Psyco或PyPy而不是Cython.