from datetime import datetime as time
a = np.random.randn(10000, 64, 8)
b = np.random.randn(8, 100)
t1 = time.now()
res1 = np.dot(a, b)
t2 = time.now()
res2 = np.zeros((10000, 64, 100))
for i in range(len(a)):
res2[i] = a[i].dot(b)
t3 = time.now()
print('With loop: {}\nWithout: {}'.format((t3 - t2).total_seconds()*1000, (t2 - t1).total_seconds()*1000))
>>>With loop: 562.9920000000001
>>>Without: 2124.908
我随机选择尺寸,但它们显示出差异。为什么这么大?同样,随着我增加数组的维数,它也只会增长。尺寸较小时,不带环的np.dot表现出比带环更好的性能。