Write your code to be simple and readable, especially for the first implementation. Code that is prematurely optimized can be unnecessarily complex without providing a significant gain in performance. Then, if speed is an issue, you can measure how long your code takes to run and profile your code to identify bottlenecks. If necessary, you can take steps to improve performance.
MATLAB handles data storage for you automatically. However, if memory is an issue, you can identify memory requirements and apply techniques to use memory more efficiently.
To identify which lines of code consume the most time or which lines MATLAB does not run, profile your code.
To determine how much of a file MATLAB executes when you profile it, run the Coverage Report.
Understand how MATLAB allocates memory to write code that uses memory more efficiently.
Reduce the amount of memory your program requires, determine the appropriate data storage, avoid fragmenting memory, and reclaim used memory.
MATLAB generates an
whenever it requests a segment of memory from the operating system
that is larger than what is available.