Main Content

Techniques to Improve Performance

To speed up the performance of your code, consider these techniques.

Environment

Be aware of background processes that share computational resources and decrease the performance of your MATLAB® code.

Code Structure

While organizing your code:

  • Use functions instead of scripts. Functions are generally faster.

  • Prefer local functions over nested functions. Use this practice especially if the function does not need to access variables in the main function.

  • Use modular programming. To avoid large files and files with infrequently accessed code, split your code into simple and cohesive functions. This practice can decrease first-time run costs.

Programming Practices for Performance

Consider these programming practices to improve the performance of your code.

  • Preallocate — Instead of continuously resizing arrays, consider preallocating the maximum amount of space required for an array. For more information, see Preallocation.

  • Vectorize — Instead of writing loop-based code, consider using MATLAB matrix and vector operations. For more information, see Vectorization.

  • Place independent operations outside loops — If code does not evaluate differently with each for or while loop iteration, move it outside of the loop to avoid redundant computations.

  • Create new variables if data type changes — Create a new variable rather than assigning data of a different type to an existing variable. Changing the class or array shape of an existing variable takes extra time to process.

  • Use short-circuit operators — Use short-circuiting logical operators, && and || when possible. Short-circuiting is more efficient because MATLAB evaluates the second operand only when the result is not fully determined by the first operand. For more information, see Short-Circuit AND and Short-Circuit OR.

  • Avoid global variables — Minimizing the use of global variables is a good programming practice, and global variables can decrease performance of your MATLAB code.

  • Avoid overloading built-ins — Avoid overloading built-in functions on any standard MATLAB data classes.

  • Avoid using “data as code” — If you have large portions of code (for example, over 500 lines) that generate variables with constant values, consider constructing the variables and saving them, for example, in a MAT-file or .csv file. Then you can load the variables instead of executing code to generate them.

  • Run code in the background — Use parfeval with backgroundPool to run a function in the background. You can simultaneously run other code in MATLAB and make your apps more responsive. For more information, see Run Functions in the Background.

  • Run code on a GPU or in parallel — If you have a Parallel Computing Toolbox™ license, run code on a GPU by passing gpuArray data to a supported function or run code in parallel using, for example, a parfor-loop. For more information, see Choose a Parallel Computing Solution (Parallel Computing Toolbox).

Tips on Specific MATLAB Functions

Consider the following tips on specific MATLAB functions when writing performance critical code.

  • Avoid clearing more code than necessary. Do not use clear all programmatically. For more information, see clear.

  • Avoid functions that query the state of MATLAB such as inputname, which, whos, exist(var), and dbstack. Run-time introspection is computationally expensive.

  • Avoid functions such as eval, evalc, evalin, and feval(fname). Use the function handle input to feval whenever possible. Indirectly evaluating a MATLAB expression from text is computationally expensive.

  • Avoid programmatic use of cd, addpath, and rmpath, when possible. Changing the MATLAB path during run time results in code recompilation.

Related Topics