To speed up the performance of your code, consider these techniques.
Be aware of background processes that share computational resources and decrease the performance of your MATLAB® code.
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.
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 Logical Operators:
Short Circuit
.
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.
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(
,
and var
)dbstack
. Run-time introspection is computationally
expensive.
Avoid functions such as eval
, evalc
, evalin
,
and feval(
. Use
the function handle input to fname
)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.