我试图理解@jitclass装饰器如何与嵌套类一起工作.我写了两个虚拟类:fifi和toto
fifi有一个toto属性.这两个类都有@jitclass装饰器,但编译失败.这是代码:
fifi.py
from numba import jitclass, float64
from toto import toto
spec = [('a',float64),('b',float64),('c',toto)]
@jitclass(spec)
class fifi(object):
def __init__(self, combis):
self.a = combis
self.b = 2
self.c = toto(combis)
def mySqrt(self,x):
s = x
for i in xrange(self.a):
s = (s + x/s) / 2.0
return s
toto.py:
from numba import jitclass,int32
spec = [('n',int32)]
@jitclass(spec)
class toto(object):
def __init__(self,n):
self.n = 42 + n
def work(self,y):
return y + self.n
启动代码的脚本:
from datetime import datetime
from fifi import fifi
from numba import jit
@jit(nopython = True)
def run(n,results):
for i in xrange(n):
q = fifi(200)
results[i+1] = q.mySqrt(i + 1)
if __name__ == '__main__':
n = int(1e6)
results = [0.0] * (n+1)
starttime = datetime.now()
run(n,results)
endtime = datetime.now()
print("Script running time: %s"%str(endtime-starttime))
print("Sqrt of 144 is %f"%results[145])
当我运行脚本时,我得到[…]
TypingError: Untyped global name ‘toto’
File “fifi.py”, line 11
请注意,如果我删除’fifi’中对’toto’的任何引用,代码工作正常,由于numba,我得到了x16加速.