for循环和numpy内置函数的效率

比较for循环和numpy内置函数的效率

import time
import numpy as np 

x1=[9, 2, 5, 0, 0, 7, 5, 0, 0, 0, 9, 2, 5, 0, 0]
x2=[9, 2, 2, 9, 0, 9, 2, 5, 0, 0, 9, 2, 5, 0, 0]

#for循环
#点乘
tib=time.clock()
ans=0
for i in range(len(x1)):
    ans+=x1[i]*x2[i]
tie=time.clock()
print("ans = " + str(ans) + "\ntime = " + str(1000*(tie-tib)) + "ms")

#生成矩阵
tib=time.clock()
mat=np.zeros((len(x1),len(x2)))
for i in range(len(x1)):
    for j in range(len(x2)):
        mat[i,j]=x1[i]*x2[j]
tie=time.clock()
print("matrix = " + str(mat) + "\ntime = " + str(1000*(tie-tib)) + "ms")

#生成向量
tib=time.clock()
vec=np.zeros(len(x1))
for i in range(len(x1)):
    vec[i]=x1[i]*x2[i]
tie=time.clock()
print("element multiplication = " + str(vec) + "\ntime = " + str(1000*(tie-tib)) + "ms")

#广播
ran=np.random.rand(3,len(x1))
print("ran shape = " + str(ran.shape))
tib=time.clock()
rdot=np.zeros(ran.shape[0])
for i in range(ran.shape[0]):
    for j in range(len(x1)):
        rdot[i]+=ran[i,j]*x1[j]
tie=time.clock()
print("random dot = " + str(rdot) + "\ntime = " + str(1000*(tie-tib)) + "ms")
ans = 278
time = 0.037ms
matrix = [[81. 18. 18. 81.  0. 81. 18. 45.  0.  0. 81. 18. 45.  0.  0.]
 [18.  4.  4. 18.  0. 18.  4. 10.  0.  0. 18.  4. 10.  0.  0.]
 [45. 10. 10. 45.  0. 45. 10. 25.  0.  0. 45. 10. 25.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [63. 14. 14. 63.  0. 63. 14. 35.  0.  0. 63. 14. 35.  0.  0.]
 [45. 10. 10. 45.  0. 45. 10. 25.  0.  0. 45. 10. 25.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [81. 18. 18. 81.  0. 81. 18. 45.  0.  0. 81. 18. 45.  0.  0.]
 [18.  4.  4. 18.  0. 18.  4. 10.  0.  0. 18.  4. 10.  0.  0.]
 [45. 10. 10. 45.  0. 45. 10. 25.  0.  0. 45. 10. 25.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
time = 0.5397ms
element multiplication = [81.  4. 10.  0.  0. 63. 10.  0.  0.  0. 81.  4. 25.  0.
0.]
time = 0.0699ms
ran shape = (3L, 15L)
random dot = [18.29194764 27.48252156 27.55980781]
time = 0.2805ms
import time
import numpy as np 

x1 = [9, 2, 5, 0, 0, 7, 5, 0, 0, 0, 9, 2, 5, 0, 0]
x2 = [9, 2, 2, 9, 0, 9, 2, 5, 0, 0, 9, 2, 5, 0, 0]

#numpy内置函数
#点乘
tib=time.clock()
dot=np.dot(x1,x2)
tie=time.clock()
print("dot = " + str(dot) + "\ntime = " + str(1000*(tie-tib)) + "ms")

#生成矩阵
tib=time.clock()
outer=np.outer(x1,x2)
tie=time.clock()
print("outer = " + str(outer) + "\ntime = " + str(1000*(tie-tib)) + "ms")

#生成向量
tib=time.clock()
mul=np.multiply(x1,x2)
tie=time.clock()
print("mul = " + str(mul) + "\ntime = " + str(1000*(tie-tib)) + "ms")

#广播
ran=np.random.rand(3,len(x1))
tib=time.clock()
gdot=np.dot(ran,x1)
tie=time.clock()
print("gdot = " + str(gdot) + "\ntime = " + str(1000*(tie-tib)) + "ms")
dot = 278
time = 0.0179ms
outer = [[81 18 18 81  0 81 18 45  0  0 81 18 45  0  0]
 [18  4  4 18  0 18  4 10  0  0 18  4 10  0  0]
 [45 10 10 45  0 45 10 25  0  0 45 10 25  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [63 14 14 63  0 63 14 35  0  0 63 14 35  0  0]
 [45 10 10 45  0 45 10 25  0  0 45 10 25  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [81 18 18 81  0 81 18 45  0  0 81 18 45  0  0]
 [18  4  4 18  0 18  4 10  0  0 18  4 10  0  0]
 [45 10 10 45  0 45 10 25  0  0 45 10 25  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]]
time = 0.0739ms
mul = [81  4 10  0  0 63 10  0  0  0 81  4 25  0  0]
time = 0.0269ms
gdot = [23.68344843 19.57739898 15.32674118]
time = 0.0236ms

你可能感兴趣的:(深度学习,python)