Code covered by the BSD License  

4.36

4.4 | 76 ratings Rate this file 147 Downloads (last 30 days) File Size: 328 KB File ID: #5685
image thumbnail

Writing Fast MATLAB Code

by

 

11 Aug 2004 (Updated )

Speed up your project with this collection of techniques.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

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

Acknowledgements

This file inspired Local Peaks and Guidelines For Writing Clean And Fast Code In Matlab.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (84)
08 Apr 2014 Emre Ozan Alkan

Awesome! Thank you!

02 Oct 2012 Manu  
29 Jul 2012 Michael  
30 Mar 2012 Olexander Zhytenko

very good document

18 Feb 2012 Olexander Zhytenko

Good, Thanks!

24 Jan 2012 Sven Koerner  
27 Jun 2011 David Smith  
28 Jan 2011 Michael Völker  
30 Dec 2010 Luis Andres

Good work!

23 Sep 2010 Savas

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

28 Apr 2010 Javier Lopez-Calderon

Great, Thanks!

03 Apr 2010 Nitin  
23 Oct 2009 Jada  
07 Aug 2009 Danila  
19 May 2009 Leonardo Glavina  
09 Jan 2009 Aniket

very well written guide..thanks

31 Dec 2008 Nguyen Thang

Good, Thanks!

29 Nov 2008 sara  
28 Nov 2008 Luigi Giaccari

Thanks sometimes pdf are better than m-file.

25 Nov 2008 Sunil

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.

11 Oct 2008 Juan Pablo Arias  
04 Oct 2008 Khanh Vo

I forgot rating. :)))

04 Oct 2008 Khanh Vo

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

28 Sep 2008 Gamal Alkirshi

Thank you about this informations

08 Sep 2008 AMIT RAJ  
02 Sep 2008 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.

14 Aug 2008 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.

07 Aug 2008 nick .

Nice little read with some random tips.

07 Aug 2008 ritesh shah  
26 Jul 2008 abiodun olaluwe

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

16 Jul 2008 taneat zaetung  
15 Jul 2008 Issac Niwas S  
06 Jul 2008 marie billa

good

19 Jun 2008 khaled darwish  
08 May 2008 Elias Pipping  
01 May 2008 chandra Guntupalli  
01 May 2008 chandra Guntupalli  
26 Mar 2008 biswa bhusan

nice

23 Mar 2008 Dan Cohn

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

15 Feb 2008 ntobeko zulu

none

01 Jan 2008 burhan rasito

wow its very exciting

01 Dec 2007 Hari Kumar

Well written article. Quite useful. Thanks.

24 Sep 2007 oktay gültekin

very usefull

thx

22 Sep 2007 dawuni mohammed

i'm a student interested in this field.

19 Sep 2007 yugandhar yadam  
06 Sep 2007 yiseth brango

excelente

03 Aug 2007 Kyoung ho Chong

Very useful tip!! I love it

20 Jul 2007 Norbert P.  
03 Jul 2007 Sam Clanton

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

12 May 2007 emanuel luduvicce  
06 Mar 2007 Robson Mariano  
02 Mar 2007 Azalin L

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

apart from that...it was descent

01 Mar 2007 Christoph Ullrich

Very good short introduction to optimizing m-files!

23 Jan 2007 Simone Rabaovic  
03 Jan 2007 Vishnuvenkatesh Dhage  
21 Dec 2006 A M  
14 Dec 2006 ali ahsan

fair try

15 Nov 2006 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.

09 Nov 2006 Alonso Morgado

Very good

12 Oct 2006 Orlando Rodríguez

Excellent stuff.

11 Oct 2006 Guillermo Ruiz

Thanks you are really good people

05 Apr 2006 asd asdf  
26 Feb 2006 Ramin Moshfegh

Great Job

07 Feb 2006 deepak panwar  
05 Feb 2006 Jq Yin  
13 Dec 2005 Syed Husain

It's a great helping tool.

30 Nov 2005 Jas Ellis

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

10 Nov 2005 Gerald Corzo  
14 Oct 2005 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)

08 Oct 2005 sivasankaran sivanandam  
07 Oct 2005 Ahmed Ayoub  
19 Sep 2005 smita parija

itis moreuseful

12 Sep 2005 maomun munmao

fillfull

11 Jul 2005 Izru Garner

Useful

04 Jul 2005 ALoser IAm

Helpful.

10 May 2005 karthik lakshmanan  
06 May 2005 O. L.

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

02 Apr 2005 Nabil nabil

very good document

24 Jan 2005 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.

05 Jan 2005 Rene Just Nielsen

A nice (and brief) collection of useful tips.

19 Aug 2004 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.

16 Aug 2004 Saeed Attarzadeh

It's great

14 Aug 2004 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.

13 Aug 2004 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
07 Sep 2005

Various fixes and revision

01 Dec 2005

Inlining Simple Functions section added

24 Jan 2006

Various revisions, integration section added

09 Jun 2006

Signal Processing section added

10 Feb 2009

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

Contact us