标签:port alt append art 参数 数组 ima nps list
c = a12+b13,a22+b23,a32+b33,·····+an2+bn3
1.用列表+循环实现,并包装成函数
2.用numpy实现,并包装成函数
3.对比两种方法实现的效率,给定一个较大的参数n,用运行函数前后的timedelta表示。
def pySum(n):
a = list(range(n))
b = list(range(0,5*n,5))
c = []
for i in range(len(a)):
c.append(a[i] ** 2 + b[i] ** 3)
return(c)
print(pySum(10))
import numpy
def npSum(n):
a = numpy.arange(n)
b = numpy.arange(0, 5 * n, 5)
c = a**2+b**3
return(c)
print(npSum(10))
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)
标签:port alt append art 参数 数组 ima nps list
原文地址:https://www.cnblogs.com/vitan/p/9737794.html