Python for loop vs list access speed

132 Views Asked by At

This code is run in python3.6.4

matrix_n = []
for i in matrix:
    matrix_n.append(np.dstack(i))

#method1
for index, a in enumerate(matrix_n):
    for row in range(100):
        for column in range(100):
            cell_arr = a[row][column]
            conn.execute("INSERT INTO DISTRIBUTIONS VALUES (?,?,?,?,?,?)", [index, row, column, np.mean(cell_arr), np.std(cell_arr), str(cell_arr)]);
#method2
for index, a in enumerate(matrix):
    for row in range(100):
        for column in range(100):
            cell_arr = []
            for b in range(len(a)):
                cell_arr.append(a[b][row][column])

            conn.execute("INSERT INTO DISTRIBUTIONS VALUES (?,?,?,?,?,?)", [index, row, column, np.mean(cell_arr), np.std(cell_arr), str(cell_arr)]);

For some reason, method2 is roughly twice as fast as method1. The initial building of matrix_n was run both times so that is a non factor in terms of speed.

If anyone could explain why the for loop is faster than the direct access of the array that would be great. For reference the shape of matrix_n is (48, 100, 100, 30) and the shape of matrix is (48, 30, 100, 100) so in the second method im simply building the array from the 2nd dimension and in the first method the array is already built.

0

There are 0 best solutions below