When iterating a list of lists in python 2.7.3 I noticed performance differences when changing the order of the iteration:
I have a list of 200 lists of 500000 strings. I then iterate in the following ways:
numberOfRows = len(columns[0])
numberOfColumns = len(columns)
t1 = time.clock()
for i in xrange(numberOfRows):
for j in xrange(numberOfColumns):
cell = columns[j][i]
print time.clock() - t1
t1 = time.clock()
for i in xrange(numberOfColumns):
for j in xrange(numberOfRows):
cell = columns[i][j]
print time.clock() - t1
The program repeatedly produces outputs similar to this:
33.97
29.39
Now I expected to have efficient random access on the lists. Where do these 4 seconds come from; is it only caching?