第五次作业0929
numpy数组及处理:效率对比
问题:
- 数列:
- a = a1,a2,a3,·····,an
- b = b1,b2,b3,·····,bn
- 求:
- c = a12+b13,a22+b23,a32+b33,·····+an2+bn3
1.用列表+循环实现,并包装成函数
2.用numpy实现,并包装成函数
3.对比两种方法实现的效率,给定一个较大的参数n,用运行函数前后的timedelta表示。
1.输入代码:
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) print(a,b,c)
运行结果:
2.输入代码:
import numpy a = numpy.arange(20) b = numpy.arange(0,60,6) print(a,b)
运行结果:
3.输入代码:
print(pySum(20)) import numpy def npSum(n): a = numpy.arange(n) b = numpy.arange(0,7*n,7) c = a**2+b**3 return (c) 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)
运行结果: