我正在寻找一种将 JIT 用于 python 类构造函数的方法,如下所示:
import numpy as np
from numbapro import jit, autojit
from time import time
class Test(object):
@jit((float, float, float), target="cpu")
def __init__(self, x, y, z):
self._x = x
self._y = y
self._z = z
@autojit
def runTest(self):
N = 1000000
self._z = 0
for i in xrange(N):
self._z = self._z + np.sin(i)
return self._z
if __name__ == '__main__':
a = Test(4,5,6)
start_time = time()
z = a.runTest()
end_time = time() # Get the CPU end time
print("Math Time: {0} s".format(end_time - start_time))
print z
但是,好像我必须给self一个类型,我不知道如何实现。
也许有人知道如何解决这个问题?
提前致谢
安迪
请您参考如下方法:
numba 从 0.12 版开始不再支持“jitting”类:here is the Github issue .曾经有 support for classes in Numba ,称为 'extension types' ,但现在所有相关示例都不起作用,给出: TypeError: 'NotImplementedType' object is not callable 如果使用当前版本 (0.17) 执行。
因此,我强烈建议您将 runTest 功能移动到它自己的函数中,并且您使用 a tuple或 a numpy array将数据传递给该函数,因为支持这些机制。否则,您将不得不使用旧版本的 numba。
class Test(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
@jit(float(float))
def runTest(z):
N = 1000000
z = 0
for i in xrange(N):
z = z + np.sin(i)
return z
if __name__ == '__main__':
a = Test(4,5,6)
start_time = time()
z = runTest(a.z)
end_time = time() # Get the CPU end time
print("Math Time: {0} s".format(end_time - start_time))
print z
