Solved

MATLAB loop speed

Posted on 2003-11-10
4
1,969 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
Comment Utility
>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
Comment Utility
0
 

Expert Comment

by:alexandros
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 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

10 Experts available now in Live!

Get 1:1 Help Now