Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Group info
Members: 34
Language: English
Group categories: Not categorized
More group info »
Recent pages and files
2). Issues on Performance    

Please search “Techniques for Improving Performance” in MATLAB help for more information. Almost all tips mentioned here can be found there.

 

1.1).       Measure performance using stopwatch timer

[e.g.]

tic

    any statements

toc

[e.g.]

for n = 1:100

    A = rand(n,n);

    b = rand(n,1);

    tic

    x = A\b;

    t(n) = toc;

end

Notice: Other related functions: cputime, clock, etime

1.2).       Vectorizing Loops

MATLAB is a matrix language, which means it is designed for vector and matrix operations. You can often speed up your M-file code by using vectorizing algorithms that take advantage of this design. Vectorization means converting for and while loops to equivalent vector or matrix operations.

[e.g.]

i = 0;

for t = 0:.01:10

    i = i + 1;

    y(i) = sin(t);

end

Improvement:

t = 0:.01:10;

y = sin(t);

Notice: Performance is extremely important when the dataset is large. Please use ‘for’ and ‘while’ loops as few as possible and always try to do vectorization, although sometimes vectorization is not so easy to do.

1.3).       Better not to Change a Variable's Data Type or Dimension

[e.g.]

x = 0;

for k = 2:1000

   x(k) = x(k-1) + 5;

end

Improvement: Change the first line to x = zeros(1, 1000);

[e.g.]

X = 23;  .

-- other code  --  .

X = 'A';                  % X changed from type double to char .

-- other code  --

Improvement: create a new variable instead of changing the type of X

1.4).       Functions Are Generally Faster Than Scripts

Your code executes more quickly if it is implemented in a function rather than a script.

1.5).       Using Appropriate Logical Operators

In if and while statements, it is more efficient to use the short-circuiting operators, && for logical AND and || for logical OR. This is because these operators often don't have to evaluate the entire logical expression. For example, MATLAB evaluates only the first part of this expression whenever the number of input arguments is less than three:

if (nargin >= 3) && (ischar(varargin{3}))

Notice: more information about short-circuiting operators is available in MATLAB help.

1.6).       Load and Save Are Faster Than File I/O Functions

If you have a choice of whether to use load and save instead of the low-level MATLAB file I/O routines such as fread and fwrite, choose the former. load and save have been optimized to run faster and reduce memory fragmentation.

Version: 
Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google