#列表+循环 实现,并包装成一个函数 def pySum(n): a = list(range(10)) b = list(range(0,5*n,5)) c = [] for i in range(len(b)): c.append(a[i]+b[i]) return (c) print(pySum(10))
运行结果:
import numpy def arySum(n): a = numpy.arange(n) b = numpy.arange(0,2*n,2) c = a+b return (c) print(arySum(10))
运行结果:
#列表实现数组相加的效率 from datetime import datetime s_sum = datetime.now() pySum(1000000) delta_sum = datetime.now()-s_sum print(delta_sum) #实现数组相加的效率numpy s_numpy = datetime.now() arySum(1000000) delta_numpy = datetime.now()-s_numpy print(delta_numpy)
运行结果: