File Exchange

image thumbnail

Writing Fast MATLAB Code

version 1.1 (328 KB) by

Speed up your project with this collection of techniques.

4.38462
79 Ratings

51 Downloads

Updated

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week Popular File 2008

Learn how to use the Profiler tool, vectorized functions, and other tricks to writing efficient MATLAB code. This article includes how to convert any array into a column vector, bounding a value without if statements, and repeating/tiling a vector without repmat.

Contents:
 * Introduction
 * The Profiler
 * Array Preallocation
 * JIT Acceleration
 * Vectorization
 * Inlining Simple Functions
 * Referencing Operations
 * Solving Ax=b
 * Numerical Integration
 * Signal Processing
 * Miscellaneous Tricks
 * Further Reading

Comments and Ratings (88)

furusuna yume

Azalin L

Hey Sam Thanks for pointing that out.

Stephen Cobeldick

A tidy introduction to writing faster MATLAB code. The PDF covers the main points to consider, the general advice given is sound, and the example code should be able to assist new MATLAB users.

However there are several mistakes, one of which actually slows code down:

* Using |find| when simple logical indexing would be faster: only two out of the nine code examples that use |find(...)| is actually required (both are in "Vectorized Logic"). In all other code examples they could be replaced by logical indexing, which would be faster and simpler code:

http://blogs.mathworks.com/steve/2008/01/28/logical-indexing/

* |ind2sub| is shown with only one output, whereas actually the row and column indices are returned in separate outputs. Thus the subscript indexing example is incorrect.

* Misleading statements like "Using preallocation with a frequently resizing cell array is even more beneficial than with double arrays": MATLAB's own documentation does not make any such statement, and clearly recommends preallocating numeric arrays. Numeric arrays are stored in contiguous memory locations, so preallocation is critical for them: this is reflected in the fact that this is the _very first topic_ on MATLAB's own "Improving Performance" page:

http://www.mathworks.com/help/matlab/matlab_prog/techniques-for-improving-performance.html

The section on preallocation does not mention some other ways to preallocate arrays, in particular:

1. Looping in reverse.

2. Allocating one scalar element at a specific location.

Both of these can be faster than allocating an entire array before commencing calculations.

* Non-standard terminology makes it difficult for readers to transfer this knowledge to MATLAB's own documentation. For example "per-element" operations are actually named "element-wise" or "array" operations by MATLAB, and what are called "indices" in this document are called "linear indices" by MATLAB:

http://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html

http://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html

* It would be significantly improved by hyperlinks to the relevant documentation webpages, e.g. for array preallocation, element-wise operations, etc., and for every function used in the code examples.

* No mention of |bsxfun|, one of the most significant and useful functions for fast and efficient vectorized code:

http://www.mathworks.com/help/matlab/ref/bsxfun.html

Aneesh

Aneesh (view profile)

This should be standard issue with every copy of MATLAB. Very useful!

Awesome! Thank you!

Manu

Manu (view profile)

Michael

very good document

Good, Thanks!

Sven Koerner

Sven Koerner (view profile)

David Smith

Michael Völker

Luis Andres

Good work!

Savas

Savas (view profile)

Thanks a lot. THis will have a big effect on my code

Great, Thanks!

Nitin

Nitin (view profile)

Jada

Jada (view profile)

Danila

Danila (view profile)

Aniket

Aniket (view profile)

very well written guide..thanks

Nguyen Thang

Good, Thanks!

sara

sara (view profile)

Thanks sometimes pdf are better than m-file.

Sunil

Sunil (view profile)

thank u for such kind information. really helpful tips and please would you suggest me that i have a problem regarding execution time of for loop which i have to execute for 262144 times and its taking too much time about 2 mins so what should i do to reduce the execution time even i have used the preallocation of resultant matrix.

Juan Pablo Arias

Khanh Vo

I forgot rating. :)))

Khanh Vo

Thanks a lot for this. A much for any people speaks MATLAB.

Gamal Alkirshi

Thank you about this informations

AMIT RAJ

Husam Aldahiyat

This is so amazing and helpful. I've been struggling with slow executing functions and million element matrices for a long time but now it's all solved. Thanks a lot Mr. and know that you've been a lot of help. A LOT.

Abel Brown

Indeed, a very good read ... should talk about things like bsxfun and how mex files compare to past and current matlab speeds. How about a section on I/O and string parsing.

nick .

Nice little read with some random tips.

ritesh shah

abiodun olaluwe

kudos guy. I believe this meet sure meet the yearning fast majority of novice MATLAB users the world over.

taneat zaetung

Issac Niwas S

marie billa

good

khaled darwish

Elias Pipping

chandra Guntupalli

chandra Guntupalli

biswa bhusan

nice

Dan Cohn

Kudos, very informative and well written. Thank you for your good efforts!

ntobeko zulu

none

burhan rasito

wow its very exciting

Hari Kumar

Well written article. Quite useful. Thanks.

oktay gültekin

very usefull

thx

dawuni mohammed

i'm a student interested in this field.

yugandhar yadam

yiseth brango

excelente

Kyoung ho Chong

Very useful tip!! I love it

Norbert P.

Sam Clanton

I love it, Azalin.. way to face off against bad grammar with even worse...

emanuel luduvicce

Robson Mariano

Azalin L

there were some grammar errors...but I guess your a French speaker

apart from that...it was descent

Christoph Ullrich

Very good short introduction to optimizing m-files!

Simone Rabaovic

Vishnuvenkatesh Dhage

A M

ali ahsan

fair try

Sarah Miles

Excellent work.
You may want to correct the submission date. Thought it was old but realised when opened it that it was writen in June 2006.

Alonso Morgado

Very good

Orlando Rodríguez

Excellent stuff.

Guillermo Ruiz

Thanks you are really good people

asd asdf

Ramin Moshfegh

Great Job

deepak panwar

Jq Yin

Syed Husain

It's a great helping tool.

Jas Ellis

A very good outline of advanced Matlab tips. Well worth reading.

Gerald Corzo

Nemo Managna

Yes it's a good idea to use the profiler. Using the profiler, I found that Pascal's recommended matrix initial code A = s(ones(m,n)) is four times slower than the simpler A(1:m,1:n) = s or A = s +zeros(m,n)

sivasankaran sivanandam

Ahmed Ayoub

smita parija

itis moreuseful

maomun munmao

fillfull

Izru Garner

Useful

ALoser IAm

Helpful.

karthik lakshmanan

O. L.

As a beginner, I have found everything clear, fast reading, and useful. Good job!

Nabil nabil

very good document

Troels Pedersen

Very good description of the indexing features of matlab. Please not that transposing a matrix using ' will complex conjungate the elements aswell. Therefore to create a row-vector by A(:)' should be done with care. Alternatively I think that transpose(A(:)) should be used.

Rene Just Nielsen

A nice (and brief) collection of useful tips.

James Alaly

Thanks, this is a great basic guide to speeding up Matlab code. I wish I had it a year ago when I started with Matlab.

As a note though, the method shown to avoid repmat is rarely faster (in Matlab 6.5) and usually slower than actually using repmat.

But thanks for the work you put into this, I printed it out and keep it next to Richard Johnson's "Matlab Programming Style Guidelines" on my ML shelf.

Saeed Attarzadeh

It's great

Pascal Getreuer

***Response to review***

I'm sorry this article wasn't helpful, perhaps it is too introductory for you. Responding to your points:

(1) ?numel? is not a function as of MATLAB 5.3 (what usually work with). It is a standard function by version 6.1.

(2) It is true that min and max operate over columns rather than entire matrices (the article includes some discussion on this). Thus the suggested methods involving min and max include matrix to vector conversion (using "A(:)" changes A to a vector). It works for matrices and n-D matrices of all sizes.

(3) The advantage of the not repmat method is that there is no function call -- it won't be significantly faster for a single tiling but can make a difference for many separate tiling operations. Furthermore, the standard m-files in MATLAB 4.0 and earlier don't include repmat, so this method is also more compatible.

(4) MEX is not a topic of discussion in this article, only mentioned. I agree it is much more difficult to write and peer and should only be a last resort.

(5) And yes, looping has a fair bit of overhead. Try the profiler on a for loop with many iterations and a one-line inner loop.

Please Remove

You should look into NUMEL rather then prod(size(x)). Also be careful about recommending any or all or even min and max. they operate on vectors and NOT the entire matrix! You suggestions about not repmat are awful. I tried each one of your examples and found that was the same speed or faster. My matrices had around 5 million numbers in them. The last thing I would recommend is the use of MEX files. While it can be faster, it will take that much longer to write, peer, and debug. Did you check the speed of looping in matlab?

Updates

1.1

Updated for R2008b, new introduction and added Ax=b section

Signal Processing section added

Various revisions, integration section added

Inlining Simple Functions section added

Various fixes and revision

MATLAB Release
MATLAB 7.7 (R2008b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video