Reputation: 23
I have a 4-dimensional array L. L.shape = [31, 13, 250, 501]
I need to perform operations which use different elements.
Method 1:
for i1 in range(31):
for i2 in range(15):
for i3 in range(250):
for i4 in range(501):
L[i1, i2, i3, i4] # some operations involving this element
Method 2:
for i2 in range(15):
for i1 in range(31):
for i4 in range(501):
for i3 in range(250):
L[i1, i2, i3, i4] # some operations involving this element
Does the speed of execution dependent on how for loops are nested?
Upvotes: 2
Views: 141
Reputation: 52008
Note that the pattern
for i in range(a):
for j in range(b):
#do stuff
involves calling the range()
function a+1
times. If a > b
, this will be less efficient than
for j in range(b):
for i in range(a):
#do stuff
You can work out that
for i in range(a):
for j in range(b):
for k in range(c):
for l in range(d):
#do stuff
calls the range function 1 + a + a*b + a*b*c
many times, which is minimized when a <= b <= c <= d
. In your case, you can work out that the difference between the most efficient and least efficient ways of looping (measured by number of range calls) is a factor of roughly 34, which is more than I had initially expected. This doesn't mean that one approach will be 34 times quicker than the other. In typical code, the body of the inmost loop dominates the execution time no matter how you manage the bookkeeping.
Having said all this, switching to NumPy (if you are not already using it) is probably the best approach for making the code run fast.
Upvotes: 3