- 安装(anaconda环境下)
conda install numba
- Demo代码:
from numba import jit
from numpy import arange
import numpy
import time
@jit
def sum2d(arr):
M, N = arr.shape
result = 0.0
for i in range(M):
for j in range(N):
result += arr[i, j]
return result
a = arange(90000).reshape(300, 300)
# print(sum2d(a))
time_a = time.time()
x = sum2d(a)
time_b = time.time()
print(x, time_b - time_a)
time_a = time.time()
x = numpy.sum(a)
time_b = time.time()
print(x, time_b - time_a)
运行表现:
可以看到,使用numba后代码速度没有提升反而下降,由此可知要正确使用numba还是很有门槛的。
标签:arr,python,科学计算,numba,time,print,import,sum2d From: https://www.cnblogs.com/devilmaycry812839668/p/18322841