Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MATLAB loop speed

Posted on 2003-11-10
4
Medium Priority
?
2,057 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

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

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
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 …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

609 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