magic-python icon indicating copy to clipboard operation
magic-python copied to clipboard

numba 示例代码报错

Open asukaminato0721 opened this issue 3 years ago • 1 comments

http://magic.iswbm.com/zh/latest/c07/c07_05.html#id1

python 3.8.5 32bit , numba 0.52.0

from numba import jit
import time


@jit
def foo(x, y):
    tt = time.time()
    s = 0
    for i in range(x, y):
        s += i
    print("Time used: {} sec".format(time.time() - tt))
    return s


print(foo(1, 100000000))
c:\Users\wuyudi\Desktop\新建 Python File (3).py:5: NumbaWarning: 
Compilation is falling back to object mode WITH looplifting enabled because Function "foo" failed type inference due to: Unknown attribute 'time' of type Module(<module 'time' (built-in)>)

File "Desktop\新建 Python File (3).py", line 7:
def foo(x, y):
    tt = time.time()
    ^

During: typing of get attribute at c:\Users\wuyudi\Desktop\新建 Python File (3).py (7)

File "Desktop\新建 Python File (3).py", line 7:
def foo(x, y):
    tt = time.time()
    ^

  @jit
c:\Users\wuyudi\Desktop\新建 Python File (3).py:5: NumbaWarning: 
Compilation is falling back to object mode WITHOUT looplifting enabled because Function "foo" failed type inference due to: Cannot determine Numba type of <class 'numba.core.dispatcher.LiftedLoop'>

File "Desktop\新建 Python File (3).py", line 9:
def foo(x, y):
    <source elided>
    s = 0
    for i in range(x, y):
    ^

  @jit
D:\Program Files\Python3.8.5\lib\site-packages\numba\core\object_mode_passes.py:151: NumbaWarning: Function "foo" was compiled in object mode without forceobj=True, but has lifted loops.

File "Desktop\新建 Python File (3).py", line 7:
def foo(x, y):
    tt = time.time()
    ^

  warnings.warn(errors.NumbaWarning(warn_msg,
D:\Program Files\Python3.8.5\lib\site-packages\numba\core\object_mode_passes.py:161: NumbaDeprecationWarning: 
Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour.

For more information visit https://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit

File "Desktop\新建 Python File (3).py", line 7:
def foo(x, y):
    tt = time.time()
    ^

  warnings.warn(errors.NumbaDeprecationWarning(msg,
Time used: 0.12804293632507324 sec
887459712

asukaminato0721 avatar Jan 07 '21 13:01 asukaminato0721

根据报错,我把 time 移出来

from numba import jit
import time


@jit
def foo(x, y):
    s = 0
    for i in range(x, y):
        s += i
    return s


tt = time.time()
print(foo(1, 100000000))
print("Time used: {} sec".format(time.time() - tt))
887459712
Time used: 0.23203539848327637 sec

运行结果不对

asukaminato0721 avatar Jan 07 '21 13:01 asukaminato0721