numpy数组及处理:效率对比

时间:2022-07-04 21:25:02
def Sum(n):          #定义一个函数(注意:格式对齐,否则会出错)
    a=list(range(n))
    b=list(range(0,50000*n,5))
    c=[]
    for i in range(len(a)):
        c.append(a[i]**2+b[i]**3)
    return c
print(Sum(20))

import numpy as py

def pySum(n):
    a=py.array(range(n))
    b=py.array(range(0,500000*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数组及处理:效率对比