标签:array new print app port \n datetime bsp sum
用数组处理:
def Sum(n): #定义一个函数(注意:格式对齐,否则会出错) a=list(range(n)) b=list(range(0,50*n,5)) c=[] for i in range(len(a)): c.append(a[i]**2+b[i]**3) return c print(Sum(20))
执行结果:
用numpy执行:
import numpy as py def pySum(n): a=py.array(range(n)) b=py.array(range(0,50*n,n)) c=[] for i in range(len(a)): c.append(a[i]**2+b[i]**3) return c print(pySum(20))
执行结果:
import datetime def new4(): now1=datetime.datetime.now() Sum(30000) now2=datetime.datetime.now() pySum(30000) now3=datetime.datetime.now() print("sum执行时间(30W数据):" , now2-now1,"\npysum数组执行时间(30W数据):" , now3-now2) new4()
执行结果:
很显然 用numpy的函数执行,速度会更快一些
标签:array new print app port \n datetime bsp sum
原文地址:https://www.cnblogs.com/sunyubin/p/9737737.html