<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255910</link>
    <title>MATLAB Central Newsreader - forloop efficiency</title>
    <description>Feed for thread: forloop efficiency</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 12 Jul 2009 01:08:01 -0400</pubDate>
      <title>forloop efficiency</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255910#664543</link>
      <author>Juliette Salexa</author>
      <description>I've been told to avoid forloops in matlab because they're extremely slow.&lt;br&gt;
consider this:&lt;br&gt;
&lt;br&gt;
clear('cap')&lt;br&gt;
tic;&lt;br&gt;
cap=ones(15,1);&lt;br&gt;
for i=1:15&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cap(i)=2;&lt;br&gt;
end&lt;br&gt;
toc;&lt;br&gt;
&lt;br&gt;
clear('cap')&lt;br&gt;
tic;&lt;br&gt;
cap=ones(15,1);&lt;br&gt;
cap(:)=2;&lt;br&gt;
toc;&lt;br&gt;
%%&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Elapsed time is 0.000088 seconds.&lt;br&gt;
Elapsed time is 0.000017 seconds.&lt;br&gt;
&lt;br&gt;
What is matlab DOING when we do cap(:)=2, is it not a for loop ?? it's more than 4x faster than a for loop.</description>
    </item>
    <item>
      <pubDate>Sun, 12 Jul 2009 01:16:02 -0400</pubDate>
      <title>Re: forloop efficiency</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255910#664544</link>
      <author>Juliette Salexa</author>
      <description>I'm guessing it has something to do with the second command being able to do everything at once, while the frist one computes sequentially.  But I only have 2 CPUs so I'm not sure how it's parallelizing it.&lt;br&gt;
&lt;br&gt;
also: &lt;br&gt;
&lt;br&gt;
clear('cap')&lt;br&gt;
tic;&lt;br&gt;
cap=ones(15,1);&lt;br&gt;
for i=1:15&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cap(i)=2;&lt;br&gt;
end&lt;br&gt;
toc;&lt;br&gt;
&lt;br&gt;
clear('cap')&lt;br&gt;
tic;&lt;br&gt;
cap=zeros(15,1);&lt;br&gt;
for i=1:15&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cap(i)=2;&lt;br&gt;
end&lt;br&gt;
toc;&lt;br&gt;
&lt;br&gt;
%%&lt;br&gt;
&lt;br&gt;
Elapsed time is 0.000080 seconds.&lt;br&gt;
Elapsed time is 0.000057 seconds.&lt;br&gt;
&lt;br&gt;
why is it more efficient to set things up as zeros than 1s ? is it because matlab's default is to set all entries to zero when making arrays, and therefore for zeros it doesn't need to do anything while for 1s it actually has to set them ??</description>
    </item>
    <item>
      <pubDate>Sun, 12 Jul 2009 01:23:10 -0400</pubDate>
      <title>Re: forloop efficiency</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255910#664545</link>
      <author>dpb</author>
      <description>Juliette Salexa wrote:&lt;br&gt;
...&lt;br&gt;
&lt;br&gt;
&amp;gt; What is matlab DOING when we do cap(:)=2, is it not a for loop ??&lt;br&gt;
&amp;gt; it's more than 4x faster than a for loop.&lt;br&gt;
&lt;br&gt;
It's calling compiled machine code to do the equivalent whereas the &lt;br&gt;
for...end loop is interpreted.&lt;br&gt;
&lt;br&gt;
Any time you can get ML to use one of the &quot;built-in&quot; functions you'll be &lt;br&gt;
ahead in performance.&lt;br&gt;
&lt;br&gt;
There's a section in the help on &quot;Optimizing Matlab Code&quot; that's worth &lt;br&gt;
reading.&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Sun, 12 Jul 2009 04:49:01 -0400</pubDate>
      <title>Re: forloop efficiency</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255910#664553</link>
      <author>Matt Fig</author>
      <description>&quot;Juliette Salexa&quot; &amp;lt;juliette.physicist@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; I've been told to avoid forloops in matlab because they're extremely slow.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Do NOT believe that is always the case.  There have been several lengthy discussions recently showing just the opposite of that statement.  For instance look at the speed of the For loop solution versus all vectorized offers here:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/matlabcentral/newsreader/view_thread/255653#664380&quot;&gt;http://www.mathworks.com/matlabcentral/newsreader/view_thread/255653#664380&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
The point is not that For loops are better always either.  The point is that, if speed is of prime concern, one must have more to go on than just a simple &quot;For loops are slow in MATLAB&quot; rule.  This &quot;rule&quot; is NOT true in general;  the nature of the problem, coupled with experience, will often inform a code designer as to which would be faster for a particular problem.  &lt;br&gt;
&lt;br&gt;
Stick around this NG long enough and you will see plenty of examples of both cases!</description>
    </item>
    <item>
      <pubDate>Sun, 12 Jul 2009 06:11:03 -0400</pubDate>
      <title>Re: forloop efficiency</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255910#664559</link>
      <author>Bruno Luong</author>
      <description>There is no trivial rule about &quot;for-loop&quot; is slow. It is quite hard to judge before hand whereas for-loop or vectorized solution is better. It needs quite a bit of experiences in Matlab in judge well, and even experience users are sometime stumbled.&lt;br&gt;
&lt;br&gt;
Few easy tricks come to mind:&lt;br&gt;
- if there is an equivalent vectorized stock function, always use it&lt;br&gt;
- avoid for-loop that call function with non negligible overhead&lt;br&gt;
- for loop is desirable when a nested IF condition can be used to save computation time&lt;br&gt;
- for loop is attractive when the result of the preceding iteration(s) can be used to save computation effort of the current calculation&lt;br&gt;
- using for loop is not recommended when the large data need to be duplicated inside the loop&lt;br&gt;
- time it, time it and time it.&lt;br&gt;
- Read Matt Fig's post!&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
  </channel>
</rss>

