Writing Fast Matlab Code #1: Introduction
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).
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 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).
Popularity: 1% [?]


















































