Note that there are some explanatory texts on larger screens.

plurals
  1. POMatrix multiplication running times Python < C++ < Matlab - Explain
    text
    copied!<p>I have a matrix <code>M</code> thats's <code>16384 x 81</code>. I want to compute <code>M * M.t</code> (the result will be <code>16384x16384</code>).</p> <p>My question is: <strong>could somebody please explain the running time differences</strong>?</p> <p>Using <strong>OpenCV in C++</strong> the following code takes <strong>18 seconds</strong></p> <pre><code>#include &lt;cv.h&gt; #include &lt;cstdio&gt; using namespace cv; int main(void) { Mat m(16384, 81, CV_32FC1); randu(m, Scalar(0), Scalar(1)); int64 tic = getTickCount(); Mat m2 = m * m.t(); printf("%f", (getTickCount() - tic) / getTickFrequency()); } </code></pre> <p>In <strong>Python</strong> the following code takes <s>only <strong>0.9 seconds</strong></s> <strong>18.8 seconds</strong> (see comment below)</p> <pre><code>import numpy as np from time import time m = np.random.rand(16384, 81) tic = time() result = np.dot(m, m.T) print (time() - tic) </code></pre> <p>In <strong>MATLAB</strong> the following code takes <strong>17.7 seconds</strong></p> <pre><code>m = rand(16384, 81); tic; result = m * m'; toc; </code></pre> <hr> <p>My only guess would have been that it's a memory issue, and that somehow Python is able to avoid swap space. When I watch <code>top</code>, however, I do not see my <code>C++ application</code> using all the memory, and I had expected that <code>C++</code> would win the day. Thanks for any insights.</p> <p><strong>Edit</strong></p> <p>After revising my examples to time only the operation, the code now takes 18 seconds with Python, also. I'm really not sure what's going on, but if there's enough memory, they all seem to perform the same now.</p> <p>Here are timings if the number of rows is 8192: C++: 4.5 seconds Python: 4.2 seconds Matlab: 1.8 seconds</p>
 

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