python - Cython cdef 没有速度增加

标签 python c cython

感谢 stackoverflow 上的一些人,我终于让 cython 工作了,但现在有一个问题。从我不使用 cdef 到我使用 cdef 确实没有速度提升。不要误会我的意思,当我使用 cython 编译 python 代码时,速度有非常明显的提升,但当我使用 cdef 时,速度提升并不明显。

这是导致程序其余部分变慢的代码区域:

def tupdate(self,surf):     

    cdef int x
    cdef int y

    for x in xrange(self.w):
        for y in xrange(self.h):
            if self.map[(x,y)].y <= 600 and self.map[(x,y)].y >= -50: self.map[(x,y)].FLAG = 1
            else: self.map[(x,y)].FLAG = 0
            self.map[(x,y)].y += self.speed
            if self.map[(x,y)].FLAG:
                self.map[(x,y)].rect = ((self.map[(x,y)].x,self.map[(x,y)].y),(50,25))
                self.map[(x,y)].update()
                self.map[(x,y)].render(surf)

这段代码应该每秒被调用 60 次,但是由于代码的速度和我的旧硬件它每秒只被调用大约 30 次,我认为使用 cdef 如此强烈使用像 x 和 y 这样的变量会有所帮助。但是cdef前后fps是一样的。

我在 Windows 7 64 位上使用 32 位 python 2.7

最佳答案

您在代码中索引 self.map 11 次,索引相同。索引它一次,将引用复制到本地名称,然后使用该名称。

关于python - Cython cdef 没有速度增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11733554/

相关文章:

python - 垃圾收集会让 python 变慢吗?

python - Cython 发出 'unnecessary' 警告

python - PyMinuit 与 IMinuit

python - PyGame 将 cStringIO 加载为图像

python - 在python中解析wget日志文件

c++ - 为什么 OpenCV 拒绝 cvLoadImage ("string.ext"),但接受 cvLoadImage(argv[1])?

Python mysqldb 错误关闭连接

c++ - 覆盖 DirectX 结构的新建和删除

c - 我在哪里可以假定空字符将在 C 中自动添加?

Python/Cython/Fortran 模块 - undefined symbol : _gfortran_runtime_error