Accelerating MATLAB Performance book

Accelerating MATLAB Performance book
CRC discount promo code

Quick links:     Reviews     Table of Contents     Book organization     About the author     Source code     Errata list

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, 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!
Use promo code MZK07 for a 25% discount and free worldwide shipping on


… 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 (use the scroll-bar on the right to see all reviews):


Scroll down to see additional reviews

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 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, 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

Additional information can be found here.

Source codes

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

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

ChapterSectionPageParagraphCurrent textCorrection typeCorrected textPost date
11.3.282nd 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)…clarificationWe 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
33.1.15792nd from bottomc1 = bsxfun(), b(1,:)');
c2 = bsxfun(), b(2,:)');
c3 = bsxfun(), b(3,:)');
fixc1 = bsxfun(@minus, a(1,:), b(1,:)');
c2 = bsxfun(@minus, a(2,:), b(2,:)');
c3 = bsxfun(@minus, a(3,:), b(3,:)');
33.2.182bottom[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));
33.3944th from top(none – new paragraph)additionIn 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 from bottom(none – new paragraph)additionIf you have the Database Toolbox, use the fastinsert function to insert multiple data records into a table in bulk mode.2015-11-18
33.6.31134th from bottom(none – new paragraph)additionWe 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
44.1.31432nd from topAs a counter-example, the standard fft function is faster for doubles than singles…fixAs another example, the standard fft function is faster for singles than for doubles (ignore the part on FFTW)2015-01-01
44.5.6177topFFT functions are generally faster for double-precision data than for singles…fixFFT functions are generally faster for single-precision data than for doubles (ignore the part on FFTW)2015-01-01
88.1.33945th from topprintf("s() called with...fixprintf("%s() called with...2016-03-13
88.1.5403footnoteSee § §
88.1.64056th from topmgGetPropertyfixmxGetProperty2015-03-10
88.5.14515th from top(none – new paragraph)additionWhen 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
99.4.3485bottom(none – new paragraph)additionNote 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
99.5.35045th from topAccesss=privatefixAccess=private2015-11-18
1010.1.25315th from bottom(none – new text)additionSmaller 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
1010.1.12.35442nd from bottom(none – new text)additionUsing 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
1010.4.3.85902nd from topif now - lastTime > 0.3*ONE_SEC
   % do some expensive GUI/graphs update
fixthisTime = now;
if thisTime - lastTime > 0.3*ONE_SEC
   % do some expensive GUI/graphs update
   lastTime = thisTime;
1111.3.46034th from top(none – new paragraph)additionIt 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
1111.4611bottom(none – new paragraph)additionWhen 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

15 Responses to Accelerating MATLAB Performance book

  1. wei says:

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

    • Yair Altman says:

      @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.

  2. Jonathan C. says:

    Should be a great resource. Congratulations on your success!

  3. Pingback: Sortie du livre « Accelerating MATLAB Performance » | Demande à la poussière

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

  5. Brad Stiritz says:

    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.

  6. Sergey says:

    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.


    • @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 (link)

  7. sepideh says:

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

  8. N/A says:

    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.

    • 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;
  9. Christoph says:

    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?



Leave a Reply

Your email address will not be published. Required fields are marked *