- Undocumented Matlab - https://undocumentedmatlab.com -

Accelerating MATLAB Performance book

Posted By Yair Altman On December 13, 2014 | 16 Comments

Accelerating MATLAB Performance book [1]
CRC discount promo code [2]

Quick links:     Reviews [3]     Table of Contents [4]     Book organization [5]     About the author [6]     Source code [7]     Errata list [8]

The Matlab programming environment is often perceived as a platform suitable for prototyping and modeling but not for “serious” applications. One of the main complaints is that Matlab is just too slow.

Accelerating MATLAB Performance (CRC Press, ISBN 9781482211290 [9], 785 pages) aims to correct this perception, by describing multiple ways to greatly improve Matlab program speed.

The book:

  • Demonstrates how to profile MATLAB code for performance and resource usage, enabling users to focus on the program’s actual hotspots
  • Considers tradeoffs in performance tuning, horizontal vs. vertical scalability, latency vs. throughput, and perceived vs. actual performance
  • Explains generic speedup techniques used throughout the software industry and their adaptation for Matlab, plus methods specific to Matlab
  • Analyzes the effects of various data types and processing functions
  • Covers vectorization, parallelization (implicit and explicit), distributed computing, optimization, memory management, chunking, and caching
  • Explains Matlab’s memory model and shows how to profile memory usage and optimize code to reduce memory allocations and data fetches
  • Describes the use of GPU, MEX, FPGA, and other forms of compiled code
  • Details acceleration techniques for GUI, graphics, I/O, Simulink, object-oriented Matlab, Matlab startup, and deployed applications
  • Discusses a wide variety of MathWorks and third-party functions, utilities, libraries, and toolboxes that can help to improve performance

Ideal for novices and professionals alike, the book leaves no stone unturned. It covers all aspects of Matlab, taking a comprehensive approach to boosting Matlab performance. It is packed with thousands of helpful tips, code examples, and online references. Supported by this active website, the book will help readers rapidly attain significant reductions in development costs and program run times.

Click here to get your book copy now [10]!
Use promo code MZK07 for a 25% discount and free worldwide shipping on crcpress.com


Amazon reviews - Accelerating MATLAB Performance [11]… a very interesting new book on MATLAB® performance … covering basic tools and an appropriate range of specific programming techniques. The book seems to take a whole-system approach … helping readers understand the big picture of how to get better performance.

—Michelle Hirsch, Ph.D., Head of MATLAB Product Management, The MathWorks Inc.

Reviews of the book on Amazon [11]

Table of Contents


CHAPTER 1: Introduction to Performance Tuning
   1.1 Why should we bother?
   1.2 When to performance-tune and when not to bother
   1.3 The iterative performance tuning cycle
   1.4 What to tune
   1.5 Performance tuning pitfalls
   1.6 Performance tuning tradeoffs
   1.7 Vertical vs. horizontal scaling
   1.8 Perceived vs. actual performance

CHAPTER 2: Profiling MATLAB Performance
   2.1 The MATLAB Profiler
   2.2 tic, toc and relatives
   2.3 Timed log files and printouts
   2.4 Non-MATLAB tools

CHAPTER 3: Standard Performance-Tuning Techniques
   3.1 Loop optimization
   3.2 Data caching
   3.3 Smart checks bypass
   3.4 Exception handling
   3.5 Improving externally-connected systems
   3.6 Processing smaller data subsets
   3.7 Interrupting long-running tasks
   3.8 Latency vs. throughput
   3.9 Data analysis
   3.10 Other techniques

CHAPTER 4: MATLAB-Specific Techniques
   4.1 Effects of using different data types
   4.2 Characters and strings
   4.3 Using internal helper functions
   4.4 Date and time functions
   4.5 Numeric processing
   4.6 Functional programming
   4.7 Object-Oriented MATLAB
   4.8 MATLAB startup
   4.9 Additional techniques

CHAPTER 5: Implicit Parallelization (Vectorization and Indexing)
   5.1 Introduction to MATLAB vectorization
   5.2 Built-in vectorization functions
   5.3 Simple vectorization examples
   5.4 Repetitive data
   5.5 Multi-dimensional data
   5.6 Real-life example: Synthetic Aperture Radar matched filter
   5.7 Effective use of MATLAB vectorization

CHAPTER 6: Explicit Parallelization Using MathWorks Toolboxes
   6.1 The Parallel Computing Toolbox – CPUs
   6.2 The Parallel Computing Toolbox – GPUs
   6.3 The MATLAB Distributed Computing Server
   6.4 Techniques for effective parallelization in MATLAB

CHAPTER 7: Explicit Parallelization by Other Means
   7.1 GPU acceleration using Jacket
   7.2 Alternative/related technologies
   7.3 Multithreading
   7.4 Spawning external processes

CHAPTER 8: Using Compiled Code
   8.1 Using MEX code
   8.2 Using the MATLAB Coder Toolbox
   8.3 Porting MATLAB algorithms to FPGA
   8.4 Deployed (compiled) MATLAB programs
   8.5 Using external libraries

CHAPTER 9: Memory-Related Techniques
   9.1 Why memory affects performance
   9.2 Profiling memory usage
   9.3 MATLAB’s memory storage and looping order
   9.4 Array memory allocation
   9.5 Minimizing memory allocations
   9.6 Memory packing
   9.7 Additional recommendations

CHAPTER 10: Graphics and GUI
   10.1 Initial graphs generation
   10.2 Updating graphs and images in real-time
   10.3 Figure window performance aspects
   10.4 GUI preparation and responsiveness
   10.5 Avoiding common pitfalls

CHAPTER 11: I/O Techniques
   11.1 Reducing the amount of I/O
   11.2 Avoiding repeated file access
   11.3 Reading and writing files
   11.4 Data compression and the save function
   11.5 Excel files (and Microsoft Office files in general)
   11.6 Image files
   11.7 Using Java and C I/O
   11.8 Searching, parsing and comparing files
   11.9 Additional aspects

APPENDIX A: Additional Resources
   A.1 Online resources
   A.2 Books
   A.3 Performance-related official MATLAB bugs
   A.4 Performance-related Code Analyzer (MLint) warnings

APPENDIX B: Performance Tuning Checklist

Book organization

This book is organized in chapters grouped by related functionality/usage. It is not necessary to read the book in order: the chapters and sections are mostly independent and stand alone. You can safely skip parts that you find difficult or uninteresting.

We begin with a theoretical description of performance tuning in Chapter 1. The discussion includes typical pitfalls, tradeoffs and considerations that need to be kept in mind before and during any tuning process.

Chapter 2 provides an overview of tools that are available in MATLAB in order to diagnose an application to determine the locations of, and reasons for, its performance hotspots. There are several different manners by which we can profile application run-time in MATLAB, and different situations may dictate different tools.

Chapters 3-11 discuss specific speedup techniques that can be used in MATLAB:

  • Chapter 3 explains standard techniques adapted from non-MATLAB programming languages.
  • Chapter 4 discusses techniques that are unique to MATLAB code.
  • Chapter 5 discusses implicit parallelization, with indexing and vectorization.
  • Chapter 6 and 7 discuss explicit parallelization using a variety of means (CPU, GPU and multi-threading).
  • Chapter 8 discusses techniques for using compiled (binary) code.
  • Chapter 9 discusses specific techniques that are memory-related. The non-trivial relationship between memory and performance is explained, and a variety of tuning techniques is presented in light of these explanations.
  • Chapter 10 discusses techniques related to graphics, GUI and user interaction.
  • Chapter 11 concludes the list of specific tuning techniques with a discussion of techniques related to I/O, particularly reading and writing files.

Chapters 3 through 11 are intended for use as a random-access reference. The sections and techniques can typically be used independently of each other. You can directly use any section or technique, without reading or using any other.

Appendix A presents online and offline resources that expand the information presented in the text and enable further research. Appendix B concludes the text by providing a non-comprehensive general checklist for performance-tuning.

Throughout the text, references are provided to enable interested readers to expand their knowledge of specific issues. Footnotes are used to clarify some points and to provide cross-references to other sections within this book; endnotes are used to provide references to related online resources. Most online references are provided in both full and shortened format, to enable easy usage when transcribed from hardcopy.

About the author

Yair Altman Yair Altman, author of the popular UndocumentedMatlab.com website, is well respected in the Matlab community as an expert on advanced MATLAB programming.

Yair’s first book, Undocumented Secrets of MATLAB-Java Programming [12], was published in 2011 to rave reviews and became the standard textbook on the subject. His many years of public contribution on MATLAB performance, plus a multitude of useful tips never before published, are now available in this highly readable volume.

Yair holds a BSc in physics and an MSc in computer science, both with high honors. Yair has over 20 years of professional software development experience at various levels of organizational responsibility, from programmer to VP R&D. He has developed systems using two dozen programming languages, on a dozen different platforms, half a dozen databases, and countless MATLAB releases.

Yair became an independent MATLAB consultant several years ago, and has never looked back. He currently assists clients world-wide in various MATLAB-related aspects: consulting, training, and programming. He can be reached at altmany@gmail.com.

Additional information can be found here [13].

Source codes

A zip file containing the source codes for all non-trivial functions in the book can be downloaded from here [14].

All the files are named s<section#>_function. For example: s6_1_7_matched_filter_spmd.m refers to the matched_filter_spmd m-function found in section 6.1.7, while s11_7_mexIO.c refers to the C-MEX function found in section 11.7. In order to run these files, you would typically need to remove the s<section#>_ prefix from their filename (i.e., creating matched_filter_spmd.m and mexIO.c).

Errata list

Last updated: 2016-03-13

Chapter Section Page Paragraph Current text Correction type Corrected text Post date
1 1.3.2 8 2nd from bottom …We might well find that having graceful degradation means that the system is sub-optimal (a bit slower) for the common scenario (no load)… clarification We might well find that having graceful degradation means that the system remains reasonably responsive at high loads at the expense of being sub-optimal (a bit slower) for the common scenario (no load) – this is definitely a tradeoff worth considering. On the other hand, we might wish to take an opposite approach, so that the common scenario achieves its higher performance goal, at the expense of uncommon load scenarios. 2015-11-18
3 3.1.15 79 2nd from bottom c1 = bsxfun(), b(1,:)');
c2 = bsxfun(), b(2,:)');
c3 = bsxfun(), b(3,:)');
fix c1 = bsxfun(@minus, a(1,:), b(1,:)');
c2 = bsxfun(@minus, a(2,:), b(2,:)');
c3 = bsxfun(@minus, a(3,:), b(3,:)');
3 3.2.1 82 bottom [uniqueVals, sortedStartIdx] = unique(data);
sortedEndIdx = [sortedStartIdx(2:end)'-1, length(data)];
for idx = 1 : length(uniqueVals)
   dataIdx = sortedDataIdx(sortedStartIdx:sortedEndIdx);
fix [uniqueVals, sortedStartIdx] = unique(sortedData);
sortedEndIdx = [sortedStartIdx(2:end)'-1, length(data)];
for idx = 1 : length(uniqueVals)
   dataIdx = sortedDataIdx(sortedStartIdx(idx):sortedEndIdx(idx));
3 3.3 94 4th from top (none – new paragraph) addition In some cases, there is no directly-callable helper function, but we can still extract the core programming logic, excluding those checks and computations that we do not specifically need. For example, when the input data is properly scaled and formatted, we can use the core logic of the polyfit function to achieve significant speedups. 2015-11-18
3 105 5th from bottom (none – new paragraph) addition If you have the Database Toolbox, use the fastinsert function to insert multiple data records into a table in bulk mode. 2015-11-18
3 3.6.3 113 4th from bottom (none – new paragraph) addition We can reduce the size of the processed data set by ignoring elements whose effect on the overall result is negligible. For example, when running a processing kernel over the data, the kernel is often Gaussian in nature, quickly dropping to negligible values away from the kernel center. In such cases we can significantly reduce the processed data size by clipping the kernel, only using values close to the center (e.g., up to 13% of the kernel center’s value, representing ±2σ, or up to 1%, which represents ±3σ). 2015-11-18
4 4.1.3 143 2nd from top As a counter-example, the standard fft function is faster for doubles than singles… fix As another example, the standard fft function is faster for singles than for doubles (ignore the part on FFTW) 2015-01-01
4 4.5.6 177 top FFT functions are generally faster for double-precision data than for singles… fix FFT functions are generally faster for single-precision data than for doubles (ignore the part on FFTW) 2015-01-01
8 8.1.3 394 5th from top printf("s() called with... fix printf("%s() called with... 2016-03-13
8 8.1.5 403 footnote See § fix See § 2015-03-17
8 8.1.6 405 6th from top mgGetProperty fix mxGetProperty 2015-03-10
8 8.5.1 451 5th from top (none – new paragraph) addition When loading shared libraries into MATLAB, it is faster (sometimes significantly) to use loadlibrary using the prototype and thunk files inputs rather than the standard header file. This avoids the need for MATLAB to recompile the header file(s) in run-time. 2015-11-18
9 9.4.3 485 bottom (none – new paragraph) addition Note that in MATLAB R2015b (8.6), the allocation mechanism has significantly changed, and so has the relative performance of the various preallocation variants. On these new releases, it is important to compare the variants carefully on your specific platform. 2015-11-18
9 9.5.3 504 5th from top Accesss=private fix Access=private 2015-11-18
10 10.1.2 531 5th from bottom (none – new text) addition Smaller markers are apparently faster than large ones.Marker performance has degraded in R2014b (HG2); it improved in R2016a, but is apparently still not back at the HG1 (R2014a) performance levels. 2015-11-18
10 544 2nd from bottom (none – new text) addition Using rgb2ind with up to 256 colors uses uint8 data while a larger number of colors uses uint16. In general, the larger the number of colors, the longer time it takes rgb2ind to process the data. For rgb2ind, it is also faster to use the non-default 'nodither' input option, but note that this may degrade the output quality. 2015-11-18
10 590 2nd from top if now - lastTime > 0.3*ONE_SEC
   % do some expensive GUI/graphs update
fix thisTime = now;
if thisTime - lastTime > 0.3*ONE_SEC
   % do some expensive GUI/graphs update
   lastTime = thisTime;
11 11.3.4 603 4th from top (none – new paragraph) addition It often happens with binary data files that the data is stored in repeated patterns. It is natural to use an iterative loop in such cases, reading the pattern elements one by one within a loop over all patterns. However, for the reason outlined above this is extremely inefficient. It is both faster and simpler to textscan in order to read all patterns and their elements in one go. 2015-11-18
11 11.4 611 bottom (none – new paragraph) addition When using the save function in MATLAB R2014b (8.4) onward, ensure that the saved data does not include any graphic handles. If it does, then the all the graphics data associated with this handle will be stored, and this could amount to many MB of data even for simple graphic objects. In R2014a and earlier, the graphic system used numeric handles, so storing them only meant storing a single number, but in MATLAB’s new graphics system (HG2) these handles became class objects that are being stored with all their associated data. This bloats the file and increases the time to save and load the file. 2015-11-18


16 Comments (Open | Close)

16 Comments To "Accelerating MATLAB Performance book"

#1 Comment By wei On December 16, 2014 @ 19:04

Great book to read !
I hope that you can share e-book with us.

#2 Comment By Yair Altman On December 16, 2014 @ 23:23

@wei – the ebook format should be available for purchase at the CRC site in early 2015. Email me if you wish to be notified when this happens.

#3 Comment By Jonathan C. On December 17, 2014 @ 10:36

Should be a great resource. Congratulations on your success!

#4 Pingback By Sortie du livre « Accelerating MATLAB Performance » | Demande à la poussière On December 19, 2014 @ 00:49

[…] Plus d’informations : Accelerating MATLAB Performance book […]

#5 Comment By Michelle Hirsch On December 22, 2014 @ 10:03

Congratulations, Yair. I wish you great success with your new book.

#6 Comment By Brad Stiritz On December 29, 2014 @ 21:48

Yair, congratulations on publishing your second book. You’re amazingly productive. Thanks for posting the discount code as well. I’m looking forward to learning a lot of helpful techniques.

#7 Comment By Sergey On February 3, 2016 @ 14:09

Dear Yair,

I am reading you book and find it very useful. I believe I have found a small bug on page 82, last code snippet. I think it should rather be something like this:

[sortedData , sortedDataIdx ] = sort(data);
[uniqueVals , sortedStartIdx] = unique(sortedData);
sortedEndIdx = [sortedStartIdx(2:end)'-1,length(data)];
for idx = 1:length(uniqueVals)
    dataIdx = sortedDataIdx( sortedStartIdx(idx) : sortedEndIdx(idx) );

Sorry if I misunderstood you code and mistakenly called it a bug.


#8 Comment By Yair Altman On February 3, 2016 @ 14:30

@Sergey – thanks for the feedback. Your correction is (well) correct 🙂 I’ve added it to the Errata table above.

Please be kind enough to provide feedback on the book at Amazon ( [15])

#9 Comment By sepideh On March 13, 2016 @ 23:47

Great book to read. It has helped me a lot. Specially chapter 9 which is about memory issues

#10 Comment By Yair Altman On March 14, 2016 @ 00:03

Thanks for the kind feedback. I would appreciate if you could add a positive comment about the book on Amazon ( [15]), for the benefit of others.

Thanks in advance!

#11 Comment By N/A On June 17, 2016 @ 22:20

Section 5.6 (Real-Life Example: SAR Matched Filter)
The function on page 265 for matched filtering does not discuss how to read the data downloaded from the mentioned AFRL Link.
The function describes the data as a structure but when I read any of the files contained within folder SN132 (or using the matlabtools vwchip or vwclut) there is no mention of a variable “phdata”.

I variable imgdata is produced which looks to be complex indicating phase data but is not a structure.
Please provide a proper example file or method to read the files from the AFRL link.

Thank you.

#12 Comment By Yair Altman On July 5, 2016 @ 08:37

The data variable used in the code is indeed a Matlab struct which is generated separately. Most of its fields are precomputed constants, and only the phase data is loaded from the AFRL data file. Here is the code snippet that Pavel Emeliyanenko used to generate the data struct for the SAR functions:

Np = 128;  % columns (azimuth points)
K  = 128;  % rows (frequency samples)

B = 0.591 * 10^9;   % bandwidth (Hz)
fc  = 9.6 * 10^9;   % center frequency (Hz)
data.dr = 0.304700; % range resolution (m)
data.dx = 0.304700; % cross-range resolution (m)
data.maxWr = data.dr*(K-1);  % maximum scene size in range direction (m)
data.maxWx = data.dx*(Np-1); % maximum scene size in cross-range direction (m)

data.deltaF = B / (K-1);
data.Nfft = K*10;

xl=-3; xr=3; yl=-3; yr=3;
nx=500; ny=500;
[data.x_mat, data.y_mat] = meshgrid(xl:stepx:xr, yl:stepy:yr);
data.z_mat = zeros(size(data.x_mat));

data.minF = ones([1,Np]) * (fc - B/2);

T = 0 : 1/(Np-1) : 1;
data.AntX = T; %T.^3-3.*T.^2-1;
data.AntY = T; %T.^5-2.*T-1;
data.AntZ = ones(size(data.AntX))/5;
% distance to SAR mo-comp. point
data.R0 = sqrt(data.AntX.^2 + data.AntY.^2 + data.AntZ.^2); 

% Read the complex phase data from the AFRL data file
fp = fopen('HB05022.002.all', 'r', 'l');
amp = fread(fp, [K,Np], 'float=>float');  % real data
ph  = fread(fp, [K,Np], 'float=>float');  % imaginary data
data.phdata = single(amp) + single(ph) * 1i;

#13 Comment By Christoph On March 22, 2017 @ 00:19

Dear Yair,
I just tried to order your book via the CRC website.
However, when I tried to enter the promo code MZK07 for a discount, I got the following message:
“Promo Code MZK07 was not applied. The discount code was not recognized or has already expired.”

Is the promo code still valid? Or is there another promo code I can use?



#14 Comment By Yair Altman On March 22, 2017 @ 10:50

@Christoph – thanks for letting me know. I’ve asked CRC about it – I will let you know when I hear back from them.

#15 Comment By Yair Altman On March 23, 2017 @ 16:47

@Christoph – I was just informed by CRC that they fixed the problem with the MZK07 promo code and it should now be active again.

#16 Comment By EHSAN On August 20, 2019 @ 09:44

dear Yair Altman

I have a problem with the MATLAB user interface, please guide me. I’ve made a project, In my project I want to call the amount of strings from the editing box and show it on the load bar diagram. The String value can be variable, and I want to automatically display this new value every time the String value changes.

help me.

thank you

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/books/matlab-performance

URLs in this post:

[1] Image: http://www.crcpress.com/product/isbn/9781482211290#img

[2] Image: http://www.crcpress.com/product/isbn/9781482211290#banner

[3] Reviews: #Reviews

[4] Table of Contents: #TOC

[5] Book organization: #Organization

[6] About the author: #Author

[7] Source code: #Sources

[8] Errata list: #Errata

[9] CRC Press, ISBN 9781482211290: http://www.crcpress.com/product/isbn/9781482211290#isbn

[10] Click here to get your book copy now: http://www.crcpress.com/product/isbn/9781482211290

[11] Image: http://www.amazon.com/Accelerating-MATLAB-Performance-speed-programs/product-reviews/1482211297?sortBy=bySubmissionDateDescending#productSummary

[12] Undocumented Secrets of MATLAB-Java Programming: http://undocumentedmatlab.com/books/matlab-java

[13] here: http://undocumentedmatlab.com/about

[14] from here: http://undocumentedmatlab.com/files/Accelerating_MATLAB_Performance_sources.zip

[15] : http://amazon.com/Accelerating-MATLAB-Performance-speed-programs/product-reviews/1482211297/ref=cm_cr_dp_see_all_summary

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.