Documentation |
On this page… |
---|
for and while loops that incrementally increase the size of a data structure each time through the loop can adversely affect performance and memory use. Repeatedly resizing arrays often requires MATLAB^{®} to spend extra time looking for larger contiguous blocks of memory, and then moving the array into those blocks. Often, you can improve code execution time by preallocating the maximum amount of space required for the array.
The following code displays the amount of time needed to create a scalar variable, x, and then to gradually increase the size of x in a for loop.
tic x = 0; for k = 2:1000000 x(k) = x(k-1) + 5; end toc
Elapsed time is 0.301528 seconds.
If you preallocate a 1-by-1,000,000 block of memory for x and initialize it to zero, then the code runs much faster because there is no need to repeatedly reallocate memory for the growing data structure.
tic x = zeros(1, 1000000); for k = 2:1000000 x(k) = x(k-1) + 5; end toc
Elapsed time is 0.011938 seconds.
Use the appropriate preallocation function for the kind of array you want to initialize:
zeros for numeric arrays
cell for character arrays
When you preallocate a block of memory to hold a matrix of some type other than double, avoid using the method
A = int8(zeros(100));
This statement preallocates a 100-by-100 matrix of int8, first by creating a full matrix of double values, and then by converts each element to int8. Creating the array as int8 values saves time and memory. For example:
A = zeros(100, 'int8');
For best performance, keep the following suggestions in mind when you assign values to variables.
If you need to store data of a different type, it is advisable to create a new variable. Changing the class or array shape of an existing variable slows MATLAB down because it takes extra time to process.
This code changes the type of X from double to char, which has a negative impact on performance:
X = 23; . -- other code -- . X = 'A'; % X changed from type double to char . -- other code --
You should not assign a real value to a variable that already holds a complex value, and vice versa. Assigning a complex number to a variable that already holds a real number impacts the performance of your program.
When performing a logical AND or OR operation, you have a choice of whether to use short-circuit operators.
Operator | Description |
---|---|
&, | | Perform logical AND and OR on arrays element by element |
&&, || | Perform logical AND and OR on scalar values with short-circuiting |
In if and while statements, it is more efficient to use the short-circuit operators, && and ||, because these operators often do not evaluate the entire logical expression. For example, whenever the first input argument is not numeric, MATLAB evaluates only the first part of this expression.
if (isnumeric(varargin{1})) && (ischar(varargin{2}))
See Logical Operators: Short Circuit for a discussion about using && and ||.
If the performance of your program remains a concern, then consider the following suggestions:
Split large script files into smaller ones, having the first file call the second if necessary.
Construct separate functions (or local functions and nested functions) from the larger chunks of code.
If you have overly complicated functions or expressions, use simpler ones to reduce size. Simpler functions often make good utility functions that you can share with others.
Use functions instead of scripts because they are generally faster.
Vectorize your code to take advantage of the full functionality of MATLAB.
Use a sparse matrix structure to vectorize code without requiring large amounts of memory.
Avoid running large processes in the background while executing your program in MATLAB.
Avoid overloading MATLAB built-in functions on any standard MATLAB data classes because this can negatively affect performance.