【发布时间】:2017-06-10 02:32:33
【问题描述】:
我正在做一些需要快速协程的事情,我相信 numba 可以加快我的代码速度。
这是一个愚蠢的例子:一个函数将其输入平方,并加上它被调用的次数。
def make_square_plus_count():
i = 0
def square_plus_count(x):
nonlocal i
i += 1
return x**2 + i
return square_plus_count
你甚至不能 nopython=False JIT 这个,大概是因为 nonlocal 关键字。
但如果您使用类,则不需要nonlocal:
def make_square_plus_count():
@numba.jitclass({'i': numba.uint64})
class State:
def __init__(self):
self.i = 0
state = State()
@numba.jit()
def square_plus_count(x):
state.i += 1
return x**2 + state.i
return square_plus_count
这至少有效,但如果你这样做nopython=True,它就会中断。
有没有可以用nopython=True 编译的解决方案?
【问题讨论】: