datetime

#用列表+循環實現,幷包裝成函數
def pySum(n): a
= list(range(10)) b = list(range(0, 50, 5)) c = [] for i in range(len(a)): c.append(a[i] ** 2 + b[i] ** 3) return(c) print(pySum(10))
#用numpy實現,
幷包裝成函數
import numpy
def npSum(n):
    a = numpy.arange(10)
    b = numpy.arange(0, 50, 5)
    c = a + b
    return(c)

print(npSum(10))



#對比兩種方法實現的效率,給定一個較大參數n,用運行函數先後的timedelta表示
from datetime import datetime start = datetime.now() pySum(100000) delta = datetime.now()-start print(delta) start= datetime.now() npSum(100000) delta = datetime.now()-start print(delta)

運行結果app

相關文章
相關標籤/搜索