?
Solved

MATLAB loop speed

Posted on 2003-11-10
4
Medium Priority
?
2,074 Views
Last Modified: 2012-06-27
I am currently programming some large scripts in matlab.  I have discovered that when using 'while' loops, or 'for' loops, etc. the processing time increases almost exponentially.  
I am looking for why loops greatly slow down the program in matlab and how to fix it without taking out the loops and just copying and pasting the code out.  Any help would be greatly appreciated.

-gkatz
0
Comment
Question by:gkatz
  • 2
4 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 375 total points
ID: 9720004
>I have discovered that when using 'while' loops, or 'for' loops, etc. the processing time increases almost exponentially.
this depends entirely on how big your loop is (how many iterations per loop)

>how to fix it without taking out the loops and just copying and pasting the code out
there are two main ways of optimizing loops ...
1. look for all statements that you can move out of the loop... there might be some calculation inside the loop which is bein used over and over again unchanged ... move it out

2. loop unrolling ::: e.g. you have a loop as
for ( i = 0; i <5 ; i ++ )
    a[i] = 7;
this loop can be completely removed by
a[0]=a[1]=.... = 7

this was a small example, but the main motive is to expand the code inside the loop and reduce the number of iterations...
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9720009
0
 

Expert Comment

by:alexandros
ID: 9721502
First of all remember that every variable in Matlab is saved as an array. That means matlab is designed for matrix and vector calculations.  It is very important to try to vectorize your code in order to make it run faster, actually very faster. Unlike compiled languages, matlab interprets each line in a for loop on each iteration of the loop. Most loops can be eliminated by performing an equivalent operation using matlab vectors. Sometimes this is very easy to do and it is a great gain speed.

e.g.
for i=0:0.1:1000
      y(i*10+1)=sin(i);
end

would run much faster if you wrote it as:

i=0:0.1:1000
y=sin(i);

O.K. this is a simple example but i do not want to copy/paste matlab help book here. Search for improoving performance and vectorizing loops in matlab documentaion. There you `ll find more complicated examples and which functions to use in order to achieve your goal.

Remember also that functions are executed much faster than scripts. So a "lazy solution" to accelerate your programs would be to write the code inside your loops as a function. Remember though that this won`t work as well as vectorizing your loops.
0
 
LVL 3

Author Comment

by:gkatz
ID: 9732447
Thanks guys for your quick response, after continuing to work on my code I found that my problem was actually in calling a function that was being passed an array.  Matlab does not reference the array like C or C++ so that was slowing down the program.  Both made good points and I found the links to be very interesting.  Thanks again.
Gkatz
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month17 days, 7 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question