python numpy高效体现
import numpy as np import time 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 start=time.time() python_sum(1000000) end=time.time() print('python',end-start) start=time.time() numpy_sum(1000000) end=time.time() print('numpy',end-start)
结果:
===================== RESTART: D:/Python/Python37/numpy1.py ====================
python 1.4060001373291016
numpy 0.018000125885009766
>>>