?
Solved

MATLAB loop speed

Posted on 2003-11-10
4
Medium Priority
?
2,043 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

752 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