Python Code Acceleration(Python代碼加速)

對於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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章