对于Python的代码执行效率较低的问题,参考博客:https://developer.51cto.com/art/201809/583695.htm进行相应的测试。
参考代码如下:
from numba import jit
import time
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))
输出时间:
Time used: 3.7836766242980957 sec
4999999950000000
加入代码:
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))
输出时间:
Time used: 0.02804708480834961 sec
4999999950000000
这个时间很可观了,very very fast!!!对于时间的进一步优化以及使用明天再来详细研究。
I hope I can help you,If you have any questions, please comment on this blog or send me a private message. I will reply in my free time.