import numpy as np
from timeit import timeit
def python_sum(n):
a=[i**2 for i in range(n)]
b=[i**3 for i in range(n)]
c=[]
for i in range(n):
c.append(a[i]+b[i])
return c
def numpy_sum(n):
a = np.arange(n) ** 2
b = np.arange(n) ** 3
return a+b
t1=timeit('python_sum(1000)','from __main__ import python_sum',number=1000)
t2=timeit('numpy_sum(1000)','from __main__ import numpy_sum',number=1000)
print(t1/t2)
标签:__,timeit,sum,01Numpy,import,对比,numpy,性能,1000
From: https://www.cnblogs.com/lyc2001/p/17062961.html