Following is the test code, my actual code looks almost similar in which i use the original matrix rather randomly generated. How can I optimize this nested for loops. I know it's possible in python but I am unable to do so.
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)
I am using python 2.7. Thank you.