Reputation: 1
Why is so much time spent on the multiplication of columns in the case of the column index using in a cycle? What can be done to reduce the execution time of this part?
8 @profile
9 def zero_one_two(grid, N):
10 23810 236265 9.9 1.1 if 1 in grid[:, 0]:
11 731 9416 12.9 0.0 grid[:, 0][np.where(grid[:, 0] > 0)[0]] = 2
12 23810 201348 8.5 0.9 left_con = grid[:, 0]*grid[:, 1]
13 23810 195165 8.2 0.9 left_true_indices = np.where(left_con > 0)[0]
14 23810 49803 2.1 0.2 if left_true_indices.size:
15 16896 68303 4.0 0.3 grid[:, 0][left_true_indices] = 2
16 16896 56940 3.4 0.3 grid[:, 1][left_true_indices] = 2
17 861832 1671442 1.9 7.5 for j in np.arange(1, N-1):
18 838022 6193150 7.4 27.8 col_mult = grid[:, j] * grid[:, j+1]
19 838022 6079750 7.3 27.3 col_mult_true_indices = np.where(col_mult > 1)[0]
20 838022 1591003 1.9 7.1 if col_mult_true_indices.size:
21 94221 337757 3.6 1.5 grid[:, j][col_mult_true_indices] = 2
22 94221 377351 4.0 1.7 grid[:, j+1][col_mult_true_indices] = 2
23 94221 653747 6.9 2.9 col_con = grid[:-1, j]*grid[1:, j]
24 94221 694403 7.4 3.1 col_con_indices = np.where(col_con == 2)[0]
25 94221 490465 5.2 2.2 col_con_indices2 = col_con_indices + 1
26 94221 352366 3.7 1.6 grid[:, j][col_con_indices] = 2
27 94221 302229 3.2 1.4 grid[:, j][col_con_indices2] = 2
28 94221 757152 8.0 3.4 col_con2 = grid[:-1, j+1]*grid[1:, j+1]
29 94221 691756 7.3 3.1 col_con2_indices = np.where(col_con2 == 2)[0]
30 94221 482827 5.1 2.2 col_con2_indices2 = col_con2_indices + 1
31 94221 414628 4.4 1.9 grid[:, j+1][col_con2_indices] = 2
32 94221 371939 3.9 1.7 grid[:, j+1][col_con2_indices2] = 2
Upvotes: 0
Views: 36
Reputation: 1327
What exactly are you trying to accomplish? It looks like you want to multiply each column with the column left of it. This multiplication can be done much faster at once by multiplying two subsets of the original grid with each other. You can also do this for the first N columns of the grid:
grid_col_mult = grid[:,0:N]*grid[:,1:N+1]
Upvotes: 1