我刚开始使用Numba,并热衷于尝试并理解它的工作原理。
我建立了一个二进制搜索功能:
def binarysearch(alist, item):
first = 0
last = len(alist) - 1
found = False
while first<=last:
midpoint = (first + last)//2
if alist[midpoint] == item:
return midpoint
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return -1
如果我按如下方式运行它:
l = list(range(10000000))
%timeit
binarysearch(l,5000)
我得到:
4.88 µs ± 315 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
如果我这样做但使用@numba.njit
,则会得到:
19.8 s ± 533 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
关于如何改善Numba的任何想法?
版本: 的Python:3.6.3 Numba:0.40.0