Solved

MATLAB loop speed

Posted on 2003-11-10
4
1,988 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 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumDigits challenge 9 123
why to use string 10 60
Using Powershell to monitor file changes then execute a command 5 157
firstChar challenge 13 107
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump 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 …

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now