运行使用numba嵌套函数

问题描述 投票:7回答:1

我试图用numba最近加速比我的代码部分的蟒蛇。我努力,而他们都与numba编译从内部功能2运行功能1,但它不工作。这里是我的代码:

import numba as nb
from math import acos
from time import time

@nb.jit("void()")
def myfunc():
    s = 0
    for i in range(10000000):
        s += acos(0.5)
    print('The sum is: ', s)


@nb.jit("void()")
def myfunc2():
    myfunc()


tic = time()
myfunc2()
toc = time()
print(toc-tic)

当我打电话myfunc()代码工作,我得到的结果更快,当我不使用numba比。然而,当我打电话myfunc2我看到这个错误:

 File "~/.spyder-py3/temp.py", line 22, in <module>
    myfunc2()

RuntimeError: missing Environment

任何人有任何想法,为什么呼吁从另一个里面是不是在这种情况下工作的功能?

python jit numba
1个回答
3
投票

Numba v0.39+

一个修复程序在v0.39介绍。由于每Release Notes

PR#2986:修复环境传播

github pull #2986了解更多详情。

Numba pre-v0.39

这是一个已知的问题。如在github issue #2411描述:

好像环境指针不能跨nopython功能正常通过。

修订如下删除print()numba功能应该解决这个问题:

import numba as nb
from math import acos
from time import time

@nb.jit("void()")
def myfunc():
    s = 0
    for i in range(10000000):
        s += acos(0.5)
    return s

@nb.jit("void()")
def myfunc2():
    return myfunc()

tic = time()
x = myfunc2()  # 10471975.511390356
toc = time()
print(toc-tic)
© www.soinside.com 2019 - 2024. All rights reserved.