Writing Fast Matlab Code #1: Introduction

September 30, 2009 by Pascal Getreuer · Leave a Comment
Filed under: Code Optimization, Tutorials 
VN:F [1.8.8_1072]
Rating: +1 (from 1 vote)
VN:F [1.8.8_1072]
Rating: 5.5/10 (2 votes cast)

Writing Fast Matlab code

1   Introduction

Matlab is a prototyping  environment, meaning  it  focuses on the  ease of development with  language flexibility, interactive debugging,  and other  conveniences lacking in performance-oriented languages like C and Fortran.  While Matlab may not be as fast as C, there  are ways to bring it closer.

Disclaimer

This is not a beginner’s tutorial to Matlab, but  a tutorial on performance.   The  methods  discussed  here are generally  fast,  but  no claim  is made on what  is fastest.  Use at your own risk.

Before beginning our main topic, let’s step back for a moment and ask ourselves what we really want.  Do we want the fastest  possible code?—if so, we should switch to a performance  language like C. Probably more accurately, we want to spend less time total from developing, debugging,  running,  and until finally obtaining results.  This section gives some tips on working smart,  not hard.

M-Lint Messages

In more recent versions of Matlab, the integrated editor automatically gives feedback on possible code errors and suggested optimizations. These messages are helpful in improving  a program  and in learning more about  Matlab.

for k  =  1:NumTrials
 
r =  rand;
 
x(k) =  runsim(r);
 
end
 
hist(x);  %   Plot histogram of simulation outputs

m-lint message:‘x’  might be growing  inside  a loop.  Consider preallocating for speed.
Hold the  mouse cursor  over underlined  code to read  a message.  Or,  see all M-Lint  messages  at  once with Tools → M-Lint → Show M-Lint Report.

Organization

  • Use  a separate folder for  each pro ject
    A separate folder for each project  keeps related  things  together  and simplifies making  copies and backups  of project  files.
  • Write header comments, especially H1.
    The  first line of the  header  comment is called the  H1 comment.   Type  help(cd) to get a listing of all project  files and their  H1 comments.
  • Save frequent  console commands as  a script.
    If you find yourself repeating  certain  commands  on the console, save them as a script.  Less typing means fewer opportunities for typos.

Avoid losing data

  • Don’t use  clear all in  a script.
    This  is an unfortunate common practice—any important variables  in the  base workspace  will be irretrievably lost.
  • Beware of clobber.
    “File clobber”  refers to the kind of data  loss when a file is accidentally overwritten with another one having  the same filename.  This phenomenon can occur with variables  as well:

    >> result =  big  operation(input1);     %   Store the result  of a time−consuming  operation
    ...
    >> result =  big  operation(input2);     %   Run  again with another input

    Variable  result was clobbered  and the first output was lost.

  • Beware of what can crash MATLAB.
    While Matlab is generally  reliable,  crashes  are possible when using third-party MEX functions or extremely  memory-intensive operations, for example,  with video and very large arrays.

Now with good working habits  covered, we begin our discussion of writing fast Matlab code. The rest of this article  is organized  by topic,  first on techniques  that are useful in general application, and next on specific computational topics (table  of contents is on the first page).

Download Full Pdf Version

1 Introduction

Matlab is a prototyping environment, meaning it focuses on the ease of development with language flexibility, interactive debugging, and other conveniences lacking in performance-oriented languages like C and Fortran. While Matlab may not be as fast as C, there are ways to bring it closer.

Disclaimer This is not a beginner’s tutorial to Matlab, but a tutorial on performance. The methods discussed here are generally fast, but no claim is made on what is fastest. Use at your own risk.

Before beginning our main topic, let’s step back for a moment and ask ourselves what we really want. Do we want the fastest possible code?—if so, we should switch to a performance language like C. Probably more accurately, we want to spend less time total from developing, debugging, running, and until finally obtaining results. This section gives some tips on working smart, not hard.

M-Lint Messages

In more recent versions of Matlab, the integrated editor automatically gives feedback on possible code errors and suggested optimizations. These messages are helpful in improving a program and in learning more about Matlab.

for k = 1:NumTrials r = rand;

x(k) = runsim(r);


end


l xl might be growing inside a loop. Consider preallocating for speed.


hist(x); % Plot histogram of simulation outputs

✿✿✿✿✿✿✿✿✿✿✿✿✿✿

Hold the mouse cursor over underlined code to read a message. Or, see all M-Lint messages at once with Tools M-Lint Show M-Lint Report.

Organization

Use a separate folder for each pro ject.

A separate folder for each project keeps related things together and simplifies making copies and backups of project files.

Write header comments, especially H1.

The first line of the header comment is called the H1 comment. Type help(cd) to get a listing of all project files and their H1 comments.

1


Save frequent console commands as a script.

If you find yourself repeating certain commands on the console, save them as a script. Less typing means fewer opportunities for typos.

Avoid losing data

Don’t use clear all in a script.

This is an unfortunate common practice—any important variables in the base workspace will be irretrievably lost.

Beware of clobber.

“File clobber” refers to the kind of data loss when a file is accidentally overwritten with another one having the same filename. This phenomenon can occur with variables as well:

>> result = big operation(input1); % Store the result of a timeconsuming operation

>> result = big operation(input2); % Run again with another input

Variable result was clobbered and the first output was lost.

Beware of what can crash MATLAB.

While Matlab is generally reliable, crashes are possible when using third-party MEX functions or extremely memory-intensive operations, for example, with video and very large arrays.

Now with good working habits covered, we begin our discussion of writing fast Matlab code. The rest of this article is organized by topic, first on techniques that are useful in general application, and next on specific computational topics (table of contents is on the first page).

VN:F [1.8.8_1072]
Rating: 5.5/10 (2 votes cast)
VN:F [1.8.8_1072]
Rating: +1 (from 1 vote)

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Live
  • PDF
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Add to favorites
  • email
  • MySpace
  • RSS