np.dot和for的计算速度比较

import numpy as np

#help(np.array)
a = np.array([1,2,3,4])
print(a)

import time

#help(np.random.rand)
a = np.random.rand(1000000)
b = np.random.rand(1000000)

tic = time.time()
c = np.dot(a,b)
toc = time.time()

print("Vectorized Version:"+str(1000*(toc - tic)))

c = 0
tic = time.time()
for i in range(1000000):
    c += a[i]*b[i]
toc = time.time()

print("For Loop:"+str(1000*(toc - tic)))

结果:

Vectorized Version:6.813764572143555
For Loop:313.1237030029297

 

你可能感兴趣的:(python3)