Accelerating MATLAB Performance : 1001 tips to speed up MATLAB programs book cover
1st Edition

Accelerating MATLAB Performance
1001 tips to speed up MATLAB programs

ISBN 9781482211290
Published December 11, 2014 by Chapman and Hall/CRC
792 Pages 31 Color & 228 B/W Illustrations

SAVE ~ $23.00
was $115.00
USD $92.00

Prices & shipping based on shipping country


Book Description

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 aims to correct this perception by describing multiple ways to greatly improve MATLAB program speed. Packed with thousands of helpful tips, it leaves no stone unturned, discussing every aspect of MATLAB.

Ideal for novices and professionals alike, the book describes MATLAB performance in a scale and depth never before published. It takes a comprehensive approach to MATLAB performance, illustrating numerous ways to attain the desired speedup.

The book covers MATLAB, CPU, and memory profiling and discusses various tradeoffs in performance tuning. It describes both the application of standard industry techniques in MATLAB, as well as methods that are specific to MATLAB such as using different data types or built-in functions.

The book covers MATLAB vectorization, parallelization (implicit and explicit), optimization, memory management, chunking, and caching. It explains MATLAB’s memory model and details how it can be leveraged. It describes the use of GPU, MEX, FPGA, and other forms of compiled code, as well as techniques for speeding up deployed applications. It details specific tips for MATLAB GUI, graphics, and I/O. It also reviews a wide variety of utilities, libraries, and toolboxes that can help to improve performance.

Sufficient information is provided to allow readers to immediately apply the suggestions to their own MATLAB programs. Extensive references are also included to allow those who wish to expand the treatment of a particular topic to do so easily.

Supported by an active website, and numerous code examples, the book will help readers rapidly attain significant reductions in development costs and program run times.

Table of Contents

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.3.1 Pareto’s Principle and the Law of Diminishing Returns
1.3.2 When to Stop Tuning
1.3.3 Periodic Performance Maintenance
1.4What to Tune
1.5Performance Tuning Pitfalls
1.5.1 When to Tune
1.5.2 Performance Goals
1.5.3 Profiling
1.5.4 Optimization
1.6 Performance Tuning Tradeoffs
1.7 Vertical versus Horizontal Scaling
1.8 Perceived versus Actual Performance
1.8.1 Presenting Continuous Feedback for Ongoing Tasks
1.8.2 Placing the User in Control
1.8.3 Enabling User Interaction during Background Processing
1.8.4 Streaming Data as it Becomes Available
1.8.5 Streamlining the Application
1.8.6 Reducing the Run-Time Variability
1.8.7 Performance and Real Time

2. Profiling MATLAB® Performance
2.1 The MATLAB Profiler
2.1.1 The Detailed Profiling Report
2.1.2 A Sample Profiling Session
2.1.3 Programmatic Access to Profiling Data
2.1.4 Function-Call History Timeline
2.1.5 CPU versus Wall-Clock Profiling
2.1.6 Profiling Techniques Relative versus Absolute Run Times Ensuring Profiling Consistency Ensuring Compatibility with Real-World Conditions Profiling GUI and I/O Code Coverage
2.1.7 Profiling Limitations
2.1.8 Profiling and MATLAB’s JIT
2.2 tic, toc and Relatives
2.2.1 The Built-In tic, toc Functions
2.2.2 Comparison between the Profiler and tic, toc
2.2.3 Related Tools
2.3 Timed Log Files and Printouts
2.4 Non-MATLAB Tools

3. Standard Performance-Tuning Techniques
3.1 Loop Optimization
3.1.1 Move Loop-Invariant Code Out of the Loop A Simple Example I/O and Memory-Related Invariants Subexpression Hoisting Loop Conditionals Invoked Functions
3.1.2 Minimize Function Call Overheads
3.1.3 Employ Early Bail-Outs
3.1.4 Simplify Loop Contents
3.1.5 Unroll Simple Loops
3.1.6 Optimize Nested Loops
3.1.7 Switch the Order of Nested Loops
3.1.8 Minimize Dereferencing
3.1.9 Postpone I/O and Graphics Until the Loop Ends
3.1.10 Merge or Split Loops
3.1.11 Loop Over the Shorter Dimension
3.1.12 Run Loops Backwards
3.1.13 Partially Optimize a Loop
3.1.14 Use the Loop Index Rather than Counters
3.1.15 MATLAB’s JIT
3.2 Data Caching
3.2.1 Read-Only Caches
3.2.2 Common Subexpression Elimination
3.2.3 Persistent Caches In-Memory Persistence Non-Memory Persistence
3.2.4 Writable Caches Initializing Cache Data Memoization Multilayered (Offline) Cache
3.2.5 A Real-Life Example: Writable Cache
3.2.6 Optimizing Cache Fetch Time
3.3 Smart Checks Bypass
3.4 Exception Handling
3.5 Improving Externally Connected Systems
3.5.1 Database Design Storage Indexing Driver and Connection SQL Queries Data Updates
3.5.2 File System and Network
3.5.3 Computer Hardware
3.6 Processing Smaller Data Subsets
3.6.1 Reading from a Database
3.6.2 Reading from a Data File
3.6.3 Processing Data
3.7 Interrupting Long-Running Tasks
3.8 Latency versus Throughput
3.8.1 Lazy Evaluation
3.8.2 Prefetching
3.9 Data Analysis
3.9.1 Preprocessing the Data
3.9.2 Controlling the Target Accuracy
3.9.3 Reducing Problem Complexity
3.10 Other Techniques
3.10.1 Coding Recursion Using Known Computational Identities Remove Unnecessary Computations ("Dead-Code" Elimination) Optimize Conditional Constructs Use Short-Circuit Conditionals (Smartly!) Multiply Rather than Divide (or Not)
3.10.2 Data Optimize the Processed Data Select Appropriate Data Structures Utilize I/O Data Compression
3.10.3 General Reduce System Interferences Self-Tuning Jon Bentley’s Rules

4. MATLAB®-Specific Techniques
4.1 Effects of Using Different Data Types
4.1.1 Numeric versus Nonnumeric Data Types
4.1.2 Nondouble and Multidimensional Arrays
4.1.3 Sparse Data
4.1.4 Modifying Data Type in Run Time
4.1.5 Concatenating Cell Arrays
4.1.6 Datasets, Tables, and Categorical Arrays
4.1.7 Additional Aspects
4.2 Characters and Strings
4.2.1 MATLAB’s Character/Number Duality
4.2.2 Search and Replace
4.2.3 Converting Numbers to Strings (and Back)
4.2.4 String Comparison
4.2.5 Additional Aspects Deblanking Concatenating Strings Converting Java Strings into MATLAB Internationalization
4.3 Using Internal Helper Functions
4.3.1 A Sample Debugging Session
4.4 Date and Time Functions
4.5 Numeric Processing
4.5.1 Using inf and NaN
4.5.2 Matrix Operations
4.5.3 Real versus Complex Math
4.5.4 Gradient
4.5.5 Optimization
4.5.6 Fast Fourier Transform
4.5.7 Updating the Math Libraries
4.5.8 Random Numbers
4.6 Functional Programming
4.6.1 Invoking Functions Scripts versus Functions Function Types Input and Output Parameters Switchyard Functions Dispatch
4.6.2 onCleanup
4.6.3 Conditional Constructs
4.6.4 Smaller Functions and M-files
4.6.5 Effective Use of the MATLAB Path
4.6.6 Overloaded Built-In MATLAB Functions
4.7 Object-Oriented MATLAB
4.7.1 Object Creation
4.7.2 Accessing Properties
4.7.3 Invoking Methods
4.7.4 Using System Objects
4.8 MATLAB Start-Up
4.8.1 The MATLAB Startup Accelerator
4.8.2 Starting MATLAB in Batch Mode
4.8.3 Slow MATLAB Start-Up
4.8.4 Profiling MATLAB Start-Up
4.8.5 Java Start-Up
4.9 Additional Techniques
4.9.1 Reduce the Number of Workspace Variables
4.9.2 Loop Over the Smaller Data Set
4.9.3 Referencing Dynamic Struct Fields and Object Properties
4.9.4 Use Warning with a Specific Message ID
4.9.5 Prefer num2cell Rather than mat2cell
4.9.6 Avoid Using containers.Map
4.9.7 Use the Latest MATLAB Release and Patches
4.9.8 Use is* Functions Where Available
4.9.9 Specify the Item Type When Using ishghandle or exist
4.9.10 Use Problem-Specific Tools
4.9.11 Symbolic Arithmetic
4.9.12 Simulink
4.9.13 Mac OS
4.9.14 Additional Ideas

5. Implicit Parallelization (Vectorization and Indexing)
5.1 Introduction to MATLAB Vectorization
5.1.1 So What Exactly is MATLAB Vectorization?
5.1.2 Indexing Techniques
5.1.3 Logical Indexing
5.2 Built-In Vectorization Functions
5.2.1 Functions for Common Indexing Usage Patterns
5.2.2 Functions That Create Arrays
5.2.3 Functions That Accept Vectorized Data reshape
5.2.4 Functions That Apply Another Function in a Vectorized Manner arrayfun, cellfun, spfun, and structfun bsxfun
5.2.5 Set-Based Functions
5.3 Simple Vectorization Examples
5.3.1 Trivial Transformations
5.3.2 Partial Data Summation
5.3.3 Thresholding
5.3.4 Cumulative Sum
5.3.5 Data Binning
5.3.6 Using meshgrid and bsxfun
5.3.7 A meshgrid Variant
5.3.8 Euclidean Distances
5.3.9 Range Search
5.3.10 Matrix Computations
5.4 Repetitive Data
5.4.1 A Simple Example
5.4.2 Using repmat Replacements
5.4.3 Repetitions of Internal Elements
5.5 Multidimensional Data
5.6 Real-Life Example: Synthetic Aperture Radar Matched Filter
5.6.1 Naïve Approach
5.6.2 Using Vectorization
5.7 Effective Use of MATLAB Vectorization
5.7.1 Vectorization Is Not Always Faster
5.7.2 Applying Smart Indexing
5.7.3 Breaking a Problem into Simpler Vectorizable Subproblems
5.7.4 Using Vectorization as Replacement for Iterative Data Updates
5.7.5 Minimizing Temporary Data Allocations
5.7.6 Preprocessing Inputs, Rather Than Postprocessing the Output
5.7.7 Interdependent Loop Iterations
5.7.8 Reducing Loop Complexity
5.7.9 Reducing Processing Complexity
5.7.10 Nested Loops
5.7.11 Analyzing Loop Pattern to Extract a Vectorization Rule
5.7.12 Vectorizing Structure Elements
5.7.13 Limitations of Internal Parallelization
5.7.14 Using MATLAB’s Character/Number Duality
5.7.15 Acklam’s Vectorization Guide and Toolbox
5.7.16 Using Linear Algebra to Avoid Looping Over Matrix Indexes
5.7.17 Intersection of Curves: Reader Exercise

6. Explicit Parallelization Using MathWorks Toolboxes
6.1 The Parallel Computing Toolbox — CPUs
6.1.1 Using parfor-Loops
6.1.2 Using spmd
6.1.3 Distributed and Codistributed Arrays
6.1.4 Interactive Parallel Development with pmode
6.1.5 Profiling Parallel Blocks
6.1.6 Running Example: Using parfor Loops
6.1.7 Running Example: Using spmd
6.2 The Parallel Computing Toolbox — GPUs
6.2.1 Introduction to General-Purpose GPU Computing
6.2.2 Parallel Computing with GPU Arrays
6.2.3 Running Example: Using GPU Arrays
6.2.4 Running Example: Using Multiple GPUs with spmd Construct
6.2.5 Executing CUDA Kernels from MATLAB
6.2.6 Running Example: Using CUDA Kernels
6.2.7 Programming GPU Using MATLAB MEX
6.2.8 Accessing GPUs from within Parallel Blocks
6.3 The MATLAB Distributed Computing Server
6.3.1 Using MDCS
6.3.2 Parallel Jobs Overview
6.3.3 Setting Up a Scheduler Interface
6.3.4 Programming Independent Jobs
6.3.5 Programming Communicating Jobs
6.3.6 Using Batch Processing on a Cluster
6.3.7 Running Example: Using Communicating Jobs on a Cluster
6.4 Techniques for Effective Parallelization in MATLAB
6.4.1 General Performance Tips
6.4.2 Performance Tips for Parallel CPU Programming
6.4.3 Performance Tips for Parallel GPU Programming

7. Explicit Parallelization by Other Means
7.1 GPU Acceleration Using Jacket
7.1.1 Key Ideas of Jacket Design
7.1.2 Jacket Interface to MATLAB
7.1.3 Using Parallel gfor Loops
7.1.4 Compiling M-Code to a CUDA Kernel with Gcompile
7.1.5 Multi-GPU Support
7.1.6 Running Example: Using Parallel gfor-Loop
7.1.7 Running Example: Using gcompile
7.1.8 Running Example: Using spmd and Multi-GPU Support
7.2 Alternative/Related Technologies
7.2.1 Using GPUmat
7.2.2 Multicore Library for Parallel Processing on Multiple Cores
7.2.3 Using ArrayFire Library via MEX Interface
7.2.4 Additional Alternatives
7.3 Multithreading
7.3.1 Using POSIX Threads
7.3.2 Using OpenMP
7.3.3 Using Java Threads
7.3.4 Using .Net Threads
7.3.5 Using MATLAB Timers
7.4 Spawning External Processes

8. Using Compiled Code
8.1 Using MEX Code
8.1.1 Introduction
8.1.2 Our First MEX Function Setting Up the Compiler The Basic Format of a MEX File "Hello, World"
8.1.3 MEX Function Inputs and Outputs
8.1.4 Accessing MATLAB Data mxArray Standard MEX API for MATLAB Data Access
8.1.5 A Usage Example
8.1.6 Memory Management
8.1.7 Additional Aspects Profiling MEX Code Calling MATLAB Functions Calling Functions in External Libraries Miscellaneous Aspects
8.2 Using the MATLAB Coder Toolbox
8.2.1 Code Adaptation
8.2.2 A Simple Example: Euclidean Distances Algorithm
8.2.3 A More Realistic Example: Dijkstra’s Shortest-Path Algorithm Compatibility Check Converting Nested Functions into Sub-Functions Converting Cell Arrays to Numeric Arrays Bypassing Unsupported MATLAB Calls Compiling the Code Using the Compiled Code
8.2.4 Configuring the Coder for Maximal Performance
8.3 Porting MATLAB Algorithms to FPGA
8.3.1 Algorithm Adaptation
8.3.2 HDL Workflow
8.3.3 Run-Time Measurements
8.4 Deployed (Compiled) MATLAB Programs
8.5 Using External Libraries
8.5.1 Introduction
8.5.2 Java
8.5.3 NAG: Numerical Algorithms Group
8.5.4 MATLAB Toolbox
8.5.5 MCT: Multi-Precision Computing Toolbox
8.5.6 Additional Libraries LightSpeed Spiral Additional Toolboxes

9. Memory-Related Techniques
9.1 Why Memory Affects Performance
9.2 Profiling Memory Usage
9.2.1 Workspace Browser
9.2.2 whos Function
9.2.3 memory Function
9.2.4 feature memstats and feature dumpmem
9.2.5 feature mtic/mtoc
9.2.6 Profiler’s Memory-Monitoring Feature
9.2.7 Profiling Java Memory
9.2.8 Using Third-Party Tools
9.2.9 Using the Operating System’s Tools
9.2.10 format debug
9.3 MATLAB’s Memory Storage and Looping Order
9.3.1 Memory Storage of MATLAB Array Data
9.3.2 Loop Down Columns Rather Than Rows
9.3.3 Effect of Subindexing
9.4 Array Memory Allocation
9.4.1 Dynamic Array Growth
9.4.2 Effects of Incremental JIT Improvements
9.4.3 Preallocate Large Data Arrays
9.4.4 Preallocation Location within the Code
9.4.5 Preallocating Nondouble Data
9.4.6 Alternatives for Enlarging Arrays
9.5 Minimizing Memory Allocations
9.5.1 MATLAB’s Copy-on-Write Mechanism Regular Variable Copies Function Input Parameters
9.5.2 In-Place Data Manipulations
9.5.3 Reusing Variables (with Utmost Care)
9.5.4 Clearing Unused Workspace Variables
9.5.5 Global and Persistent Variables
9.5.6 Scoping Rules and Nested Functions
9.5.7 Passing Handle References (Not Data) to Functions
9.5.8 Reducing Data Precision/Type
9.5.9 Devectorizing Huge Data Operations
9.5.10 Assign Anonymous Functions in Dedicated Wrapper Functions
9.5.11 Represent Objects by Simpler Data Types
9.6 Memory Packing
9.7 Additional Recommendations
9.7.1 MATLAB Variables
9.7.2 Java Objects

10. Graphics and GUI
10.1 Initial Graphs Generation
10.1.1 Reduce the Number of Plotting Elements Do Not Plot Hidden Elements Do Not Plot Overlapped Elements Use the Scatter Plot with More Than 100 Data Points Use NaN or Inf Data Values to Reduce the Number of Line Segments Use Only End-Points When Plotting Straight (Linear) Lines
10.1.2 Use Simple or No Plot Markers
10.1.3 Use Vectorized Data for Plotting
10.1.4 Use Static Axes Properties
10.1.5 Only Use Drawnow When You Are Finished Plotting
10.1.6 Use Low-Level Rather Than High-Level Graphic Functions
10.1.7 Generate Plots While the Figure Is Hidden
10.1.8 Apply Data Reduction to Plotted Data to Fit the Display Area
10.1.9 Reuse Plot Axes
10.1.10 Avoid Using the Axes Function
10.1.11 Use the Painters Figure Renderer with Fast Axes DrawMode
10.1.12 Images Reduce Image File Size Use Integer Image Data Use Direct Color Mapping (Indexed Images) Cropping
10.1.13 Patches and Volume Surfaces
10.1.14 Colorbars
10.1.15 Legends
10.1.16 Reopen Presaved Figure and/or Plot Axes
10.2 Updating Graphs and Images in Real Time
10.2.1 Axes Update
10.2.2 Plot and Image Update
10.2.3 Legends and Colorbars
10.2.4 Accessing Object Properties
10.2.5 Listeners and Callbacks
10.2.6 Trading Accuracy for Speed
10.2.7 Avoid Update to the Same Value
10.2.8 Cache Graphic Handles
10.3 Figure Window Performance Aspects
10.3.1 Use Hardware-Accelerated OpenGL Renderer and Functionality
10.3.2 Set a Nondefault WVisual/XVisual Property Value
10.3.3 Disable BackingStore
10.3.4 Disable DoubleBuffer
10.3.5 Set a Manual DitherMapMode on Old Platforms
10.3.6 Reuse Figure Windows
10.3.7 Sharing Data between GUI Callback Functions
10.4 GUI Preparation and Responsiveness
10.4.1 Creating the Initial GUI Create and Update GUI Components in Nonvisible Mode Reuse Existing Figure Windows and GUI Components Reduce the Number of Property Updates Prevent Unnecessary Callback Invocations Postpone Nongraphical Setup and Updates Make the GUI Simple to Use uitable Considerations
10.4.2 Presenting User Feedback Psychology of User Feedback Feedback in MATLAB Applications Feedback in MATLAB Slider Controls Temporarily Disabling User Interaction Feedback for Long-Duration Tasks Feedback for High-Volatility Data Reducing Intermediate Updates
10.4.3 Performing Asynchronous Actions Update GUI Selectively Reduce Callback Execution Time Prevent Callback Re-entrancy Disable Control Callbacks during Automated Updates Prefer Asynchronous Callbacks to Synchronous Polling Optimize Polling Performance Zoom and Pan Mouse Movement and Hover
10.5 Avoiding Common Pitfalls
10.5.1 Minimize Intentional Pauses
10.5.2 Delete Unused Graphic Objects

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.3.1 Text versus Binary Format
11.3.2 Text File Pre-Processing
11.3.3 Memory-Mapped Files
11.3.4 Reading Files Efficiently
11.3.5 Writing Files Efficiently
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.8.1 Searching for Files
11.8.2 Parsing and Scanning Files
11.9 Additional Aspects
11.9.1 Using p-Code
11.9.2 Network I/O
11.9.3 Miscellaneous

Appendix A: Additional Resources
Online Resources
Performance-Related Official MATLAB Bugs
Performance-Related Code Analyzer (MLint) Warnings

Appendix B: Performance Tuning Checklist



View More

Featured Author Profiles

Author - Yair  Altman

Yair Altman

MATLAB expert consultant,

Learn more about Yair Altman »


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