Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The performance difference is not related to the sizes of the matrices, but to the order in which you fill the data:</p> <pre><code>b[i,:,:] = np.random.rand(2000,2000) b[:,:,i] = np.random.rand(2000,2000) </code></pre> <p>In the first case, you are filling cells that are contiguous in memory. In the second case, cells are scattered across memory.</p> <p>When items are in contiguous memory, all neighbour cells will probably be cached when the first of them is fetched. In the other case, when one is fetched, most of its neighboring cells that will be kept in cache will not be used.</p> <p>For ilustration purposes, let's consider the two-dimensional case, and let's assume that two items fit in cache. The following matrix:</p> <pre><code>numpy.array('[[10, 20, 30], [40, 50, 60]]') </code></pre> <p>is stored like this in memory:</p> <pre><code>10 20 30 40 50 60 </code></pre> <p>Let's see what happens when we iterate over it in row order:</p> <pre><code>a[0][0] → fetch 10 from memory (cached: 10 20) a[0][1] → read 20 from cache a[0][2] → fetch 30 from memory (cached: 30 40) a[1][0] → read 40 from cache a[1][1] → fetch 50 from memory (cached: 50 60) a[1][2] → read 60 from cache </code></pre> <p>Now, let's iterate in column order:</p> <pre><code>a[0][0] → fetch 10 from memory (cached: 10 20) a[1][0] → fetch 40 from memory (cached: 30 40) a[2][1] → fetch 20 from memory (cached: 10 20) a[0][1] → fetch 50 from memory (cached: 50 60) a[1][2] → fetch 30 from memory (cached: 30 40) a[1][2] → fetch 60 from memory (cached: 50 60) </code></pre> <p>So, in the first case, you can iterate over the whole matrix using only three memory accesses, and in the second case, you need six. As a rule of thumb, reading a value from memory is ~200 times slower than reading it from cache.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload