Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason this is happening is related to the <a href="http://www.matlabtips.com/matlab-is-no-longer-slow-at-for-loops/">JIT</a> compiler, which is optimizing the MATLAB for loop. You can disable/enable the JIT accelerator using <code>feature accel off</code> and <code>feature accel on</code>. When you disable the accelerator, the times change dramatically.</p> <p>MATLAB with accel on: <code>Elapsed time is 0.009407 seconds.</code></p> <p>MATLAB with accel off: <code>Elapsed time is 0.287955 seconds.</code></p> <p>python: <code>time cost = 0.0511920452118</code></p> <p>Thus the JIT accelerator is directly causing the speedup that you are noticing. There is another thing that you should consider, which is related to the way that you defined the iteration indices. In both cases, MATLAB and python, you used Iterators to define your loops. In MATLAB you create the actual values by adding the square brackets (<code>[]</code>), and in python you use <code>range</code> instead of <code>xrange</code>. When you make these changes</p> <pre><code>% MATLAB for i = [1:1000] for j = [1:1000] # python for r in range(1000): for c in range(1000): </code></pre> <p>The times become</p> <p>MATLAB with accel on: <code>Elapsed time is 0.338701 seconds.</code></p> <p>MATLAB with accel off: <code>Elapsed time is 0.289220 seconds.</code></p> <p>python: <code>time cost = 0.0606048107147</code></p> <p>One final consideration is if you were to add a quick computation to the loop. ie <code>t=t+1</code>. Then the times become</p> <p>MATLAB with accel on: <code>Elapsed time is 1.340830 seconds.</code></p> <p>MATLAB with accel off: <code>Elapsed time is 0.905956 seconds.</code> (Yes off was faster)</p> <p>python: <code>time cost = 0.147221088409</code></p> <p>I think that the moral here is that the computation speeds of for loops, out-of-the box, are comparable for extremely simple loops, depending on the situation. However, there are other, numerical tools in python which can speed things up significantly, numpy and PyPy have been brought up so far.</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. 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