[英]Numba bad performance for a simple for loop (Python 3.10)
my code:我的代码:
from numba import njit
from functools import wraps
import time
def timeit(my_func):
@wraps(my_func)
def timed(*args, **kw):
tstart = time.time()
output = my_func(*args, **kw)
tend = time.time()
print('"{}" took {:.3f} ms to execute\n'.format(my_func.__name__, (tend - tstart) * 1000))
return output
return timed
@timeit
@njit
def calculate_smth(a,b):
result = 0
for i_a in range(a):
for i_b in range(b):
result = result + i_a + i_b
return result
if __name__ == "__main__":
value = calculate_smth(1000,1000)
without the numba decorator my function completes in ~62ms, with njit decorates (after compiling beforehand) it needs ~370ms.没有 numba 装饰器,我的 function 在 ~62ms 内完成,使用 njit 装饰(预先编译后)需要 ~370ms。 Can someone explain what I am missing?
有人可以解释我错过了什么吗?
JIT stands for Just-In-Time - meaning the code is compiled at execution time - as opposed to AOT - Ahead Of Time. JIT 代表 Just-In-Time - 意味着代码在执行时编译 - 而不是 AOT - Ahead Of Time。 As you can read in Numba docs , by default compilation is lazy, ie.
正如您在Numba docs中所读到的,默认情况下编译是惰性的,即。 it happens on the first function execution in a program.
它发生在程序中第一次执行 function 时。
It also supports AOT compilation, as described here它还支持 AOT 编译,如此处所述
Another option would be cache=True
parameter passed to numba.njit
decorator.另一种选择是将
cache=True
参数传递给numba.njit
装饰器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.