代码:
import time
import numpy as np
a = np.random.rand(1000000)
b = np.random.rand(1000000)
tic = time.time() //当前时间
c = np.dot(a,b)
toc = time.time()
print©
print(“Vactorized version:” + str(1000*(toc-tic)) + “ms”)
c = 0
tic = time.time()
for i in range(1000000):
c += a[i]*b[i]
toc = time.time()
print©
print(“for loop:” + str(1000*(toc-tic)) + “ms”)
截图:向量化是非向量化用时的300多倍