Speed up double for loop in numpy
<p>I currently have the following double loop in my Python code:</p>

<pre><code>for i in range(a):
    for j in range(b):
        A[:,i]*=B[j][:,C[i,j]]   
</code></pre>

<p>(A is a float matrix. B is a list of float matrices. C is a matrix of integers. By matrices I mean m x n np.arrays.</p>

<p>To be precise, the sizes are: A: mxa B: b matrices of size mxl (with l different for each matrix) C: axb. Here m is very large, a is very large, b is small, the l's are even smaller than b
)</p>

<p>I tried to speed it up by doing</p>

<pre><code>for j in range(b):
    A[:,:]*=B[j][:,C[:,j]]
</code></pre>

<p>but surprisingly to me this performed worse. </p>

<p>More precisely, this did improve performance for small values of m and a (the "large" numbers), but from m=7000,a=700 onwards the first appraoch is roughly twice as fast.</p>

<p>Is there anything else I can do?</p>

<p>Maybe I could parallelize? But I don't really know how.</p>

<p>(I am not committed to either Python 2 or 3)</p>