#列表+循环 实现,并包装成一个函数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 numpydef arySum(n): a = numpy.arange(n) b = numpy.arange(0,2*n,2) c = a+b return (c)print(arySum(10))
运行结果:
#列表实现数组相加的效率from datetime import datetimes_sum = datetime.now()pySum(1000000)delta_sum = datetime.now()-s_sumprint(delta_sum)#实现数组相加的效率numpys_numpy = datetime.now()arySum(1000000)delta_numpy = datetime.now()-s_numpyprint(delta_numpy)
运行结果: