【发布时间】:2014-07-10 04:14:44
【问题描述】:
我有一个 Python 程序,我想使用 Cython 加速它,同时保持 Python 解释器可读的源代码(“纯 Python 模式”)。只需按原样编译,我就可以提高 20% 的速度。然后我创建了一个 .pxd 文件并尝试在整个 .pyx 脚本中添加一些“@cython.locals(...)”来修复变量的类型。但是我发现添加这两个功能之前和之后的运行时间没有差异。 所以我有三个类似的问题:
首先,我在 .pyx 中添加了:
@cython.locals(start=cython.int, end=cython.int)
class C(object):
def __init__(self, start, end)
self.start = start
self.end = end
这有什么作用吗?
其次,如果我的 .pyx 中有一个类
class Counter(object):
def __init__(self):
self.n = 0
def __call__(self, x):
self.n += 1
我可以做些什么来修复“n”的类型(就像在“cdef class”之后的“cdef int n”一样)?
最后,如果我的 .pyx 中有一个函数:
def count():
<do something with variables a,b,c>
在 .pxd 中写入以下内容会改变什么吗? :
cpdef inline count():
cdef:
int a
int b
int c
提前感谢您的帮助。
【问题讨论】: