Geo*_*ohn 4 python numpy numba
这是我从图像构造最小树的代码(f是由scipy提供的图像)
这是我写的缝纫雕刻计划的基础.
此代码段在普通python中按预期工作.当我@numba.jit没有使用时nopython=True,它也可以工作(性能提高约200%!),但这是在对象模式下.
当我尝试使用nopython=True模式时,它不会编译,我收到错误:
Failed at nopython (nopython frontend)
Undefined variable '$313.3'
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这不会编译,因为我没有看到任何可能未定义的东西.
from numba import jit
from scipy import misc
import numba
f = misc.face()
@jit(nopython=True)
def explorethisx(inar, x):
places = []
places.append((x,0))
x1,y1 = x,0
s = numba.int64(0)
co = 0
#for _ in range( 799):
while co != numba.int16(799):
co += 1
a1,a2,a3 = 999,999,999
a1 = inar[y1 + 1][x1-1][1]
a2 = inar[y1 + 1][x1][1]
a3 = inar[y1 + 1][x1 + 1][1]
m = a1
ch = -1
if m > a2:
m = a2
ch = 0
if m > a3:
m = a3
ch = 1
x1 = x1 + ch
y1 = y1 + 1
s += inar[y1][x1][1]
places.append((x1,y1))
return([s, places])
explorethisx(f,3)
explorethisx.inspect_types()
Run Code Online (Sandbox Code Playgroud)
Numba是一个非常酷的项目,即使在python对象模式下,我也对性能改进印象深刻.
异常消息具有误导性.只是numba只支持同类列表,所以当你尝试返回时[s, places],返回一个包含一个"整数"和一个"整数元组列表"的列表,它不再是同类的.
请注意,这个最小的例子已经证明了异常:
from numba import jit
@jit(nopython=True)
def test():
places = []
places.append((1, 2))
places.append((2, 3))
return [10, places]
>>> test()
...
TypingError: Failed at nopython (nopython frontend)
Undefined variable '$0.12'
Run Code Online (Sandbox Code Playgroud)
你可以简单地返回一个元组:
return (s, places)
Run Code Online (Sandbox Code Playgroud)
而不是旧的
return([s, places])
Run Code Online (Sandbox Code Playgroud)
即使这个编译 - 该函数在调用函数时包含一个越界内存访问(我有一个段错误),所以你肯定也需要检查你的内存访问.