L
L
Lyova Matyushkin2016-01-14 13:46:38
Python
Lyova Matyushkin, 2016-01-14 13:46:38

When multiplying columns in NumPy by column index, there is a large multiplication time (time profiling applied). How can I fix it?

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tsarevfs, 2016-01-14
@tsarevfs

Most likely matrices are stored as an array of strings. Therefore, when working with columns, you have to actively jump in memory and the efficiency of the processor cache drops. You can try to transpose the matrix before active work with columns, and multiply rows.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question