下面是测试代码,我的实际代码看起来几乎相似,其中我使用的原始矩阵是随机生成的。如何优化此嵌套for循环。我知道在python中是可能的,但是我不能这样做。
import time
import numpy as np
a = 1000
b = 500
sum2,sum3,sum4 = 0
t0 = time.time()
x = np.random.random(a*a).reshape([a,a])
for outer1 in xrange(0,a):
for inner1 in xrange(0,b):
for outer2 in xrange(0,a):
for inner2 in xrange(0, a):
sum2 += x[outer2][inner2] #this is not the only operation I have
for outer3 in xrange(0,a):
for inner3 in xrange(0, a):
sum3 += x[outer3][inner3] #this is not the only operation I have
for outer4 in xrange(0,a):
for inner4 in xrange(0, a):
sum4 += x[outer4][inner4] #this is not the only operation I have
print time.time() - t0
print 'sum2: '+str(sum2)+' sum3: '+str(sum3)+' sum4: '+str(sum4)
我使用的是python2.7。
谢谢您。