Thread Subject: forloop efficiency

Subject: forloop efficiency

From: Juliette Salexa

Date: 12 Jul, 2009 01:08:01

Message: 1 of 5

I've been told to avoid forloops in matlab because they're extremely slow.
consider this:

clear('cap')
tic;
cap=ones(15,1);
for i=1:15
    cap(i)=2;
end
toc;

clear('cap')
tic;
cap=ones(15,1);
cap(:)=2;
toc;
%%


Elapsed time is 0.000088 seconds.
Elapsed time is 0.000017 seconds.

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.

Subject: forloop efficiency

From: Juliette Salexa

Date: 12 Jul, 2009 01:16:02

Message: 2 of 5

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.

also:

clear('cap')
tic;
cap=ones(15,1);
for i=1:15
    cap(i)=2;
end
toc;

clear('cap')
tic;
cap=zeros(15,1);
for i=1:15
    cap(i)=2;
end
toc;

%%

Elapsed time is 0.000080 seconds.
Elapsed time is 0.000057 seconds.

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 ??

Subject: forloop efficiency

From: dpb

Date: 12 Jul, 2009 01:23:10

Message: 3 of 5

Juliette Salexa wrote:
...

> 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.

It's calling compiled machine code to do the equivalent whereas the
for...end loop is interpreted.

Any time you can get ML to use one of the "built-in" functions you'll be
ahead in performance.

There's a section in the help on "Optimizing Matlab Code" that's worth
reading.

--

Subject: forloop efficiency

From: Matt Fig

Date: 12 Jul, 2009 04:49:01

Message: 4 of 5

"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message
> I've been told to avoid forloops in matlab because they're extremely slow.


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:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/255653#664380

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 "For loops are slow in MATLAB" rule. This "rule" 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.

Stick around this NG long enough and you will see plenty of examples of both cases!

Subject: forloop efficiency

From: Bruno Luong

Date: 12 Jul, 2009 06:11:03

Message: 5 of 5

There is no trivial rule about "for-loop" 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.

Few easy tricks come to mind:
- if there is an equivalent vectorized stock function, always use it
- avoid for-loop that call function with non negligible overhead
- for loop is desirable when a nested IF condition can be used to save computation time
- for loop is attractive when the result of the preceding iteration(s) can be used to save computation effort of the current calculation
- using for loop is not recommended when the large data need to be duplicated inside the loop
- time it, time it and time it.
- Read Matt Fig's post!

Bruno

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com